stevepedwards.com/DebianAdmin linux mint IT admin tips info

Using Crontab to Automate Commands on Schedule and with Poweroff

man crontab

crontab - maintain crontab files for individual users (Vixie Cron)

SYNOPSIS

crontab [ -u user ] file

crontab [ -u user ] [ -i ] { -e | -l | -r }

DESCRIPTION

crontab is the program used to install, deinstall or list the tables

used to drive the cron(8) daemon in Vixie Cron. Each user can have

their own crontab, and though these are files in

/var/spool/cron/crontabs, they are not intended to be edited directly.

You may want to poweroff your PC automatically at the end of the day, or at a specific time, or run other commands like backups after hours etc. and crontab can set this for you.

It can be run by anyone depending on entries in the /etc/cron.allow or /etc/cron.deny if those files are created.

Each user has their own file which is initially created using

crontab -e

This opens the default editor in Raspbian, but Mint gives a choice of editors, so for user “root” on the first file creation:

root@AMDA8 /home/stevee $ crontab -e

no crontab for test - using an empty one

Select an editor. To change later, run 'select-editor'.

1. /bin/ed

2. /bin/nano <---- easiest

3. /usr/bin/vim.basic

4. /usr/bin/vim.tiny

Choose 1-4 [2]:

- and a command and time schedule is input under each column e.g:

Once you choose your editor, or change it after with:

select-editor

you can add a cron job

# Edit this file to introduce tasks to be run by cron.

#

# Each task to run has to be defined through a single line

# indicating with different fields when the task will be run

# and what command to run for the task

#

# To define the time you can provide concrete values for

# minute (m), hour (h), day of month (dom), month (mon),

# and day of week (dow) or use '*' in these fields (for 'any').#

# Notice that tasks will be started based on the cron's system

# daemon's notion of time and timezones.

#

# Output of the crontab jobs (including errors) is sent through

# email to the user the crontab file belongs to (unless redirected).

#

# For example, you can run a backup of all your user accounts

# at 5 a.m every week with:

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

#

# For more information see the manual pages of crontab(5) and cron(8)

#

# m h dom mon dow command

00 17 * * * /sbin/poweroff

This means the "poweroff" command will run at 17:00 hrs every day of every month.

Note my poweroff command uses the full path – many googles about this – people not being able to use "shutdown", "poweroff" or variants of these, which seem to need the full path (and be root), yet other commands like “echo” do not. I thought this was because of the $PATH variables for the user, but on checking, /sbin IS in the root's path anyway, so that 's not why this command fails to run on it's own in crontab:

AMDA8 stevee # $PATH

bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory

I also thought this was maybe due to currently running processes at a higher priority preventing cron running poweroff for safety/continuity reasons, but even running:

nice -n -20 poweroff

which is the highest priority (-20 to +19) a command can run, it still did not run in crontab, as it needs a full path.

The crontab file's headings respectively are:

minutes (m) ; hours (h, 24hr) ; day of month (dom); month (mon); day of week (dow); command

Once entered, the file is saved using CtrlO using Raspian's cron editor's menu commands at the screen bottom, and exited with Ctrl X, or your own Mint editors usual commands – vim say. This editor choice will be your default for any amendments later with crontab -e.

The file can be re-read and checked with :

crontab -l

The file is saved by default (in Raspbian and Mint) under the user's name in:

ls -als /var/spool/cron/crontabs/

4 -rw------- 1 root crontab 1119 Sep 17 16:31 /var/spool/cron/crontabs/root

The user opens their own file, and – as usual - root can view or change other's crontab files by specifying the username using:

crontab -e -u (username)

Now it's just a matter of waiting until cron checks the clock and runs.

Cron will mail the user with a report – handy for debugging – as in the poweroff command example above, as I got told that the “file did not exist” (i.e. the command) so cron just did not find poweroff.

Run mail to check if its installed – if not, install and run it for local delivery only, following the screen prompts:

test@AMDA8 /home/stevee $ mail

The program 'mail' is currently not installed. To run 'mail' please ask your administrator to install the package 'mailutils'

apt-get install mailutils

Check cron is working with a simple test first set at least a minute or 2 in the future, as cron checks per minute to see if a job is spooled e.g:

tty

/dev/pts/2

crontab -e

# m h dom mon dow command

13 19 * * * date > /dev/pts/2

Hopefully, you get the output of the “date” command at the scheduled time, so cron works.

AMDA8 stevee # Thu Sep 17 19:13:01 BST 2015

Now you can schedule those daily incremental backups with rsync - locally and remotely e.g.

rsync -e ssh --progress /home/stevee/* stevee@hpmint:/home/stevee/ --exclude='Videos/' --delete-excluded --exclude='.' -vahn

Or to backup your home dir where the user folder has not been created yet on a local backup drive:

rsync ~/* /media/stevee/982adb91-5828-4997-9c8c-c846c889b018/stevee/ --exclude='.' -vahn

sending incremental file list
created directory /media/stevee/982adb91-5828-4997-9c8c-c846c889b018/stevee
Contacts/
Contacts/Alex mot.vcf

.....etc....

ALWAYS dry-run (-n) any rsync command before you run it to prevent deletion errors. The above command creates a near mirror of my user's home directory excluding the .*cache etc files, and a large Video collection. Very useful.

For a crontab line at 3pm, which you won't be watching so don't need --progress or -vhn; only -a to archive:

0 15 * * * rsync -e ssh  /home/stevee/* stevee@hpmint:/home/stevee/ --exclude='Videos/' --delete-excluded --exclude='.' -a

Check your mail for the summary of the job - if mailutils is installed.

Comments are closed.

Post Navigation