Exécution_périodique_d_un_script_avec_cronjob__centos___english_

Periodic execution of a script with cronjob

Often it is useful to be able to schedule the execution of processes in a scheduled way, for example to launch backups or to send emails. cronjob allows you to do this.

Ensure that crond.service is active and enabled:

systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (]8;;file://centos/usr/lib/systemd/system/crond.service/usr/lib/systemd/system/crond.service]8;;; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-10-22 18:44:38 UTC; 52min ago
 Main PID: 795 (crond)
    Tasks: 1 (limit: 12523)
   Memory: 1.9M
   CGroup: /system.slice/crond.service
           └─795 /usr/sbin/crond -n

The service must be in an active (running) state.

Create a script /usr/local/bin/loadavg.sh which will simply generate a log:

sudo bash -c 'cat <<EOF> /usr/local/bin/my-job.sh
#!/bin/bash
echo "Helloworld" >> /var/log/my-job.log
EOF'

Make it executable:

sudo chmod a+x /usr/local/bin/my-job.sh

Run the script:

sudo /usr/local/bin/my-job.sh

Check that the script has written data to the /var/log/my-job.log file:

sudo cat /var/log/my-job.log
Helloworld

Now, let’s delete this log:

sudo rm /var/log/my-job.log

Create a cronjob`` that executes/usr/local/bin/my-job.sh` once per minute between 8AM and 5PM and between monday and friday:

sudo crontab -e

Add:

# Min  Hour  DoM  Month  DoW  Command
*      *     *    *      *    /usr/local/bin/my-job.sh

Save then check that the addition has been taken into account:

sudo crontab -l
# Min  Hour  DoM  Month  DoW  Command
*      *     *    *      *    /usr/local/bin/my-job.sh

Check that the crond.service is running :

systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (]8;;file://centos/usr/lib/systemd/system/crond.service/usr/lib/systemd/system/crond.service]8;;; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-10-22 18:44:38 UTC; 59min ago
 Main PID: 795 (crond)
    Tasks: 1 (limit: 12523)
   Memory: 1.9M
   CGroup: /system.slice/crond.service
           └─795 /usr/sbin/crond -n

After a few minutes, check that the job has gone well. One line should indicate that the script /usr/local/bin/my-job.sh has run:

sudo tail /var/log/cron
Oct 22 19:45:39 centos crontab[3964]: (root) REPLACE (root)
Oct 22 19:45:39 centos crontab[3964]: (root) END EDIT (root)
Oct 22 19:45:41 centos crontab[3968]: (root) BEGIN EDIT (root)
Oct 22 19:45:45 centos crontab[3968]: (root) END EDIT (root)
Oct 22 19:45:52 centos crontab[3972]: (root) LIST (root)
Oct 22 19:46:01 centos crond[795]: (root) RELOAD (/var/spool/cron/root)
Oct 22 19:46:01 centos CROND[3990]: (root) CMD (/usr/local/bin/my-job.sh)
Oct 22 19:46:48 centos crontab[4012]: (root) LIST (root)
Oct 22 19:47:01 centos CROND[4028]: (root) CMD (/usr/local/bin/my-job.sh)
Oct 22 19:48:01 centos CROND[4053]: (root) CMD (/usr/local/bin/my-job.sh)

Then check that the log file has been created:

sudo cat /var/log/my-job.log
Helloworld

The Helloworld message must be displayed.

You can now schedule jobs to run at a specific time or periodically.

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×