Posted on 10th Jan 2025
To run a shell script daily using crontab on Linux , follow these steps:
.sh
extension, e.g., daily_script.sh
. Make sure the script has execute permissions by running chmod +x daily_script.sh
.sudo crontab -e
. This will launch your default text editor.minute hour day month day_of_week command
For a daily run at midnight, use:0 0 * * * /path/to/daily_script.sh
Replace /path/to/daily_script.sh
with the actual path to your script file.Here's a breakdown of the fields:
minute
: 0 (runs at the beginning of the minute)hour
: 0 (midnight)day
: *
(any day of the month)month
: *
(any month)day_of_week
: *
(any day of the week)command
: the path to your shell script
Save and exit: Save the changes to the crontab file and exit the editor.
/var/log/cron
or /var/log/syslog
) to ensure the job is running correctly.Tips:
chmod +x
) and is located in a directory accessible by the cron daemon.>/dev/null 2>&1
at the end of the command line.Example crontab entry:
0 0 * * * /home/user/daily_script.sh > /dev/null 2>&1
This will run the daily_script.sh
script at midnight every day, and redirect any output to /dev/null
to suppress email notifications.