Using cron jobs is a good way to automate tasks on a Linux system. Below is an example of a cron job on a Linux system.
Minute | Hour | Day | Month | Weekday | Command | Actions |
---|---|---|---|---|---|---|
7 | 0 | * | * | * | /usr/local/cpanel/3rdparty/bin/perl /usr/local/cpanel/3rdparty/quickinstall/scripts/checkupdates.pl |
Here is another example.
15 10 * * * /home/jcartwright/Documents/test.sh
This will run a script every day at 10:15 AM.
Run the crontab -e command to open an editor and create a new crontab.
I created a new empty crontab like this.
(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 ~ $ crontab -e
no crontab for jcartwright - using an empty one
crontab: installing new crontab
Then I added the first line and the crontab is ready.
(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Documents $ crontab -e
crontab: installing new crontab
Look in the /var/log/cron file to see the crontab activity. You may need to be the root user to view this.
Mar 14 09:54:37 localhost crontab[24885]: (jcartwright) BEGIN EDIT (jcartwright) Mar 14 09:55:54 localhost crontab[24885]: (jcartwright) REPLACE (jcartwright) Mar 14 09:55:54 localhost crontab[24885]: (jcartwright) END EDIT (jcartwright) Mar 14 09:56:24 localhost crontab[24948]: (jcartwright) BEGIN EDIT (jcartwright) Mar 14 09:57:31 localhost crontab[24948]: (jcartwright) REPLACE (jcartwright) Mar 14 09:57:31 localhost crontab[24948]: (jcartwright) END EDIT (jcartwright) Mar 14 09:58:01 localhost crond[1587]: (jcartwright) RELOAD (/var/spool/cron/jcartwright) Mar 14 10:01:01 localhost CROND[25032]: (root) CMD (run-parts /etc/cron.hourly) Mar 14 10:01:01 localhost run-parts[25035]: (/etc/cron.hourly) starting 0anacron Mar 14 10:01:01 localhost run-parts[25041]: (/etc/cron.hourly) finished 0anacron Mar 14 10:01:01 localhost CROND[25031]: (root) CMDEND (run-parts /etc/cron.hourly)
And now my script has run automatically using cron.
Mar 14 10:01:01 localhost CROND[25031]: (root) CMDEND (run-parts /etc/cron.hourly) Mar 14 10:15:01 localhost CROND[25531]: (jcartwright) CMD (/home/jcartwright/Documents/test.sh) Mar 14 10:15:01 localhost CROND[25528]: (jcartwright) CMDOUT (Current Date and Time: Thu Mar 14 10:15:01 AEDT 2024) Mar 14 10:15:01 localhost CROND[25528]: (jcartwright) CMDEND (/home/jcartwright/Documents/test.sh) [root@2403-4800-25af-b00--2 log]#
This is how easy it is to run a script using cron and automate a repetitive Linux task. Have fun and experiment with this.