Posted: 14 March 2024. At: 10:23 AM. This was 4 months ago. Post ID: 19359
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Use cron jobs on Linux to automate repetitive tasks on Linux.

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.

MinuteHourDayMonthWeekdayCommandActions
70***/usr/local/cpanel/3rdparty/bin/perl /usr/local/cpanel/3rdparty/quickinstall/scripts/checkupdates.pl
This cron job runs at 7 minutes past midnight. This is every day of every month. This is to check for Perl updates every day. This calls the Perl executable and a script to check for the aforementioned updates.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.