In Ubuntu use Cron and Crontab to Run Schedule Tasks
Open/Close/Restart Cron in Ubuntu
Config file usurally is /etc/init.d/cron
Start:
1 | sudo /etc/init.d/cron start |
Close:
1 | sudo /etc/init.d/cron stop |
Restart:
1 | sudo /etc/init.d/cron restart |
Reload config:
1 | sudo /etc/init.d/cron reload |
Create a cron task
Normally we use “crontab -u username -e” to add new task in the cron config file.
If edit the exists task, the same command.
running command line
example to write “hello world” to /home/tonytan/helloworld.txt every 5 seconds.
1 | */5 * * * * echo "hello world" >> /home/tonytan/helloworld.txt |
use shell file
1 | */5 * * * * /home/tonytan/helloworld.sh |
Write the following codes into /home/tonytan/helloworld.sh
1 | #!/bin/sh |
Note: after edit the config file everytime must restart cron, unless unuseable.
1 | sudo service cron restart |
Attached is the normally example in cron config:
1 | every 5 sec run task: |