Scheduled a task
Published on: 22nd Apr 2014
Updated on: 12th Jun 2023
Overview
To run a task periodically by scheduler is going to be helpful in relieving a person from his work. So that this person may do other work that cannot be automated.
From the business point of view, scheduler helps in reducing cost and achieving higher efficiency and effectiveness. From the individual point of view, a scheduler helps a person in doing more things efficiently.
Here's how you schedule a task
-
To view the scheduled tasks, run this command,
sudo crontab -l
Or to view the scheduled tasks for current user,
crontab -l
12.Jun.2023 - or to view the scheduled tasks for a specific user,
crontab -u user1 -l
-
For example, I want to backup user files to another directory daily,
cd /etc/cron.daily
First to create a "backup.sh" bash file,
cd ~ sudo nano backup.sh
In the "backup.sh" bash file, it looks like this,
#!/bin/sh rsync -az /home /mnt/backup2
Where the first line is called "she-bang" ("#!") which indicates that it is an bash shell script.
Once you are done, press CTRL+X. Press Y to confirm saving.
The final step is to give permission for the bash file to execute,
sudo chmod 777 backup.sh
Notes: for file permission, please refers to File & Directory.
To test the script manually,
. backup.sh
-
To create a schedule task that runs at specific time, run the following command to edit the schedule,
sudo crontab -e
Then, add a new line with a "schedule pattern" and "command/script file" (you will find the details in the crontab file itself).
For example, run the
backup.sh
at 12am,0 0 * * * /home/tester/backup.sh
Another example is to print current date time every 5 minutes,
*/5 * * * * date
-
To check if the scheduled task is running
sudo tail /var/log/syslog
-
You may save your script file into the following directories and it will run as root,
-
/etc/cron.daily
-
/etc/cron.hourly
-
/etc/cron.monthly
-
/etc/cron.weekly
-
/var/spool/cron
- this directory contains the scheduled tasks for the users. -
To view the scheduled time for the above directories, run the following command:
sudo cat /etc/crontab
-
Use cases
-
For automatic updates in your Ubuntu server, please refers to the following page: https://www.cyberciti.biz/faq/how-to-set-up-automatic-updates-for-ubuntu-linux-18-04/
-
To schedule
goaccess
for generating web traffic report, please refers to Install Nginx -
Backup user's files.
-
To check the free storage space on a monthly basis. Send an email alert when the free space is less than 20%.
-
Generates a report and sends it to the recipient. We do this a lot in our business applications.
-
Send an email reminder about a friend's birthday. Perhaps, the reminder has already been taken over by our smartphones in this era.
-
Consolidates multiple spreadsheets from various business outlets and generates a report (for example, daily sales report).
References
- For more details about schedule task, please visit: https://help.ubuntu.com/community/CronHowto
Related posts
Jump to #UBUNTU blog
Author
Lau Hon Wan, software developer.