Follow this guide to using crontab in Linux. The following is a list of cron directories:
- If you don’t want to worry about when your script runs and just want it to run hourly, daily, weekly, or monthly then just copy your script to one of those directories with a permission of
755and it will run:
Open a terminal and copy your shell script
script.shinto one of the directories above. If you need to run the script hourly, place your script file in the
cron.hourlyfolder. For daily, place it inside the
cron.dailyand so forth.
- Give the shell script the correct permission. For example, if script is called
script.sh, set permission as follows:
cd /etc/cron.daily/ chmod 755 script.sh
- Then, later in the logs you can see it ran:
1 2 3
cat /var/log/cron* # specifically cron-20200106 Jan 6 11:32:01 servername run-parts(/etc/cron.daily): starting script.sh Jan 6 11:32:01 servername run-parts(/etc/cron.daily): finished script.sh
If you prefer to have a more dynamic and explicit way to run a job, use crontab:
This opens vi editor for you. Create the cron command using the following syntax:
- The number of minutes after the hour (0 to 59)
- The hour in military time (24 hour) format (0 to 23)
- The day of the month (1 to 31)
- The month (1 to 12)
- The day of the week(0 or 7 is Sun, or use name) - The command to run
An example command would be
0 0 * * * /etc/cron.daily/script.sh. This would mean that the shell script will exactly execute at midnight every night. I personally would use an online generator to learn the syntax.
To save the changes to the crontab that you just made, hit ESC key, and then type :w followed by :q to exit.
After exiting crontab, the scheduled task (Windows equivalent) will run. If you want to check the jobs, check first as your normal user and then check as root (run
ls -lto see the script’s owner) :
1 2 3 4 5 6 7 8
# To list existing cron jobs: crontab -l crontab -u username -l # To remove an existing cron job: crontab –e # Delete the line that contains your cron job # Save and quit => Type :wq!