Manage Cron Job

TIPS & TRICKS

To search the contents of the man pages

# man -k searchterm 
# man -K searchterm

Cron job tasks

To keep in mind:

# systemctl status crond.service
# systemctl enable crond.service
# systemcdl restart crond.service

Fin the full path by using the which command and use it in the crontab:

# which <cmd>

Exemple:

# which echo
/usr/bin/echo

Files to check:

# cat /var/log/cron     // it shows list of cronjobs
# cat /etc/crontab // it shows how cronjob works

Cron job 1

| Set a Cron job that execute the script /backup.sh every days at midnight.

Of course you need to put x right on the script.

Set the cron:

# crontab -e
0 0 * * * /MyFullPath/backup.sh

Cron job 2

| Set a Cron job for harry on 12.30 at noon print /bin/echo on “hello”.
| Create a crontab for a new user natasha that runs at 14:23 everyday and executes echo “hello”.

Set the cron (-e) for user (-u) harry:

# crontab -e -u harry
30 12 * * * /bin/echo “hello”

Verify:

# crontab -l -u harry

Set the cron (-e) for user (-u) natasha:

# crontab -e -u natasha
23 14 * * * /bin/echo hello

Verify:

crontab -l -u natasha

Cron job 3

| Configure a cron job that runs every 2 minutes and executes the following command:
| - logger “EX200 in progress” at the user natasha

Set the cron (-e) for user (-u) harry:

# crontab -e -u natasha
*/2 * * * * /usr/bin/logger "EX200 in progress $(date)"

Verify:

# crontab -l -u Krish

Check if log message are being loggged in /var/log/messages:

# grep EX200 /var/log/messages
node1 natasha[28082]: EX200 in progress Sat Nov 16 10:22:01 AM CET 2024
node1 natasha[28086]: EX200 in progress Sat Nov 16 10:24:01 AM CET 2024
node1 natasha[28097]: EX200 in progress Sat Nov 16 10:26:01 AM CET 2024

Cron job 4

| Create a Daily Cron Job at 4:27pm for the derek
| User that Runs cat /etc/redhat-release and Redirects the Output to /home/derek/release.txt

Normaly, you dont’t have to create the file /home/derek/release.txt and configure chmod, chown and chgroup.

Set the cron (-e) for user (-u) derek:

# crontab -e -u derek
27 4 * * * /usr/bin/cat /etc/redhat-release >> /home/derek/releases.txt

Verify:

crontab -l -u derek

Cron job 6

| Create a periodic job that appends the current date to the file ~/tracking every 5 minutes every Sunday and Wednesday between the hours of 3am and 4am.
| Remove the ability of bob to create cron jobs

Set the cron:

# crontab -e
*/5 03-04 * * 0,3 echo $(date) >> ~/tracking

If the file doesn’t exist:

# touch ~/tracking 

Remove the ability of bob to create cron jobs (just put the user to deny in it):

# echo "bob" >> /etc/cron.deny 

Cron job 7

| Create a cron job running as root, starting at 11PM every day and writing a report on daily system resource consumption in the /var/log/consumption.log file.

Command use to gather system statistics is called sysstat and may not be installed on the system. So first what we have to do is install it and then enable the service:

# yum install sysstat
# systemctl enable sysstat
# systemctl start sysstat

Edit crontab:

# crontab -e
0 23 * * * /usr/bin/sar -A > /var/log/consumption.log

As usual it is wise to check what we have already in the system. To see crontab we just issue:

crontab -l

Additional comment:

The log file does not need to exist - it will be created automatically.
Of course root user can edit crontab of every user with flag -u specifying user name.
It is worth remembering that there are /etc/cron.allow and /etc/cron.deny

Documentations

Internet
Git
ChatGPT

> Partager <