-
Notifications
You must be signed in to change notification settings - Fork 1
S. Cron and Process clean up
For those who do not know or for those who want to refresh their memory, as per Wikipedia, cron or cron job is a time-based job scheduler in Unix computer operating systems. It automates system maintenance or administration and it's most suitable for scheduling repetitive tasks.
These tasks can be simple commands (as we will later see) or even complex scripts that need to be ran at certain hours to, let's say, generate new web-scraped files that will be posted to your website.
The actions in the cron are driven by a crontab (cron table) which specifies when and what tasks should run in a given schedule. Users can edit/view/remove crontab through a set of specific commands and even though the file is usually stored in: /var/spool/cron/crontabs/ or in etc/ in separate piece as well, you should never try and edit these files.
ubuntu:etc/ :ls cron*
crontab
cron.d:
anacron e2scrub_all php popularity-contest
cron.daily:
0anacron apport bsdmainutils dpkg man-db popularity-contest
apache2 apt-compat cracklib-runtime logrotate mlocate update-notifier-common
cron.hourly:
cron.monthly:
0anacron
cron.weekly:
0anacron man-db update-notifier-common
First time users first need to set up the editor of their choice, so if you run $ crontab -l
and when it says no crontab for sk - using an empty one
, you must set up an editor.
$ crontab -e
$ no crontab for sk - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano
2. /usr/bin/vim.basic <-- easiest, basic vim.
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [2]:
Press Shift+X to Exit.
crontab -e Edit crontab file, or create one if it doesn’t already exist.
crontab -l crontab list of cronjobs , display crontab file contents.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
Very important to memorise below structure!
I highly recommend using the crontab guru to test your configuration because I know how painful and frustrating is to wait for a cronjob to run and then it does nothing.
* * * * * /home/ubuntu/Desktop/test.sh <-- run test.sh script every minute
*/10 * * * * /home/ubuntu/Desktop/test.sh <-- run test.sh script every 10 minutes
*/10 * * * * /home/ubuntu/Desktop/test.sh > /home/ubuntu/Desktop/test.log <-- run test.sh every 10 minutes and append results to test.log
*/10 * * * * /home/ubuntu/Desktop/test.sh >> /home/ubuntu/Desktop/test.log <-- run test.sh every 10 minutes and overwrite results to test.log
*/10 * * * * /home/ubuntu/Desktop/test.sh 2> /home/ubuntu/Desktop/test_error.log <-- run test.sh every 10 minutes and write only errors to test_error.log