6 years ago
Over the years I have used Cron Jobs to schedule operations on my computer. These jobs can be for a variety of tasks, like backups of data or, in one of my special jobs, filing away CCTV pictures to a special folder at midnight every day (see my Tutorial on using Motion https://community.linuxmint.com/tutorial/view/560). Although there are plenty of tutorials which provide the syntax of a Cron Job, I haven’t yet found one that shows the novice how to do it. (Yes, I know, I’m now going to be bombarded with links!!) Before the advent of Linux Mint 18 this never mattered, as there was Gnome-Schedule to do this work easily in a GUI, but, since the release of Linux Mint 18, Gnome-Schedule is no longer in the repos. And so, hence this tutorial.
The Job to be done
Before you start, I believe it best to write a script of the job you wish to automate. This way you can quickly debug the program prior to running it as a scheduled Cron Job. Below is the text of my script I use to file away the daily CCTV pictures in movepic.sh.
mv --force *.jpg ~/archives/$DIRECTORY
This text can be created in the text editor of your choice and saved as filename.sh in the root of your home folder. (In my case this is /home/ray/. Please note, this example assumes both the folders “webcam” and “archives” already exist in the home folder. The script could have been fashioned to automatically create these folders but, as these bits would only be used once, I considered it a bit pointless!
To run and test any script file like the above, start your Terminal and type:
> sh filename.sh
where filename is the name of your script, in my case here it would be sh movepic.sh. If it works correctly, you will be ready to go on to the next stage.
Scheduling – using Crontab
Now we come to the scheduling part of this exercise. Before I would have started Gnome-Schedule, set the time of the event and provide sh movepic.sh as the operation to be undertaken. Today this needs to be done through the Terminal and the command crontab. I use two flags with this command, -l (list) and -e (edit). The main command is crontab -e to start the text editor with your personal Cron file and write your cron job to.
Because, on the computer I’m writing this tutorial there were no jobs listed, I was asked to select an editor to use. In my case I used the default editor Nano which was No.2 in the list I was given by the Terminal. So, now the screen of the Terminal displays…
(Now then, at this point I was going to insert screenshots but, as I don’t have webspace it just cannot be done, so you will need to follow this tutorial with your own Terminal open. Sorry!)
> crontab -e
The Terminal enters into Editor mode with you personal Crontab file open. As you can see, all the commands are shown at the bottom of the Terminal Window. Unfortunately, the programmers of GNU Nano have used quaint English here and ^O (Ctrl +O), Write Out means save! Hey Ho!
Bring the flashing Cursor down to the bottom of the Terminal Window using the Down Arrow (Cursor Control). Then you can type the entry for your Cron Job. In my case the job runs from a minute to midnight, so I write “59 23 * * * sh movepic.sh”.
59 23 * * * sh movepic.sh
In the above, the digit groups are separated by spaces, including the stars which denote any value. I then use a Tab space to separate the When Fields and the Command Field (sh movepic.sh).
Once happy with your entry, you need to save it (Ctrl+O), Write Out. Just use the default entry for the file name and press Enter. Now press (Ctrl+X) and so exit back from the Editor to the Terminal. Keying in crontab -l will now show your new Cron Job and you can confirm if all looks okay, or not.
> crontab -l
In my case I had to wait until the next day to ensure everything was working correctly but, please remember, your computer needs to be running at the time the Cron Job is due, for the actions to be undertaken.
For detailed description of the Cron Job Syntax, have a look at https://community.linuxmint.com/tutorial/view/1072 from kejs.
Thanks @Hammer459, I'll give it a try when I get some time free.
Sorry, no diagrams or screenshots in the above Tutorial but I have no way to upload the files in the editor being used on the Community Website.