john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Cron schedule scripts crontab list user

sudo ls -ahl /var/spool/cron

script to list all crontabs of all users

for u in `cat /etc/passwd | cut -d":" -f1`; do crontab -l -u $u; done

for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done

crontab parameters

crontab -l          //Lists the current cron jobs
crontab -e          //Edit your current crontab file using nano or vi (Ctrl X or esc :q)
crontab -r          //Remove the crontab file.
crontab -v          //Displays the last time you edited your crontab file.

* * * * * /bin/execute/this/script.sh

the five stars (with a space in between each!) represent wildcards:

1. every minute
2. every hour
3. every day of the month
4. every month
5. every day in the week

So a job you want to run at 13:02 on the 28th of the Dec (no matter what calendar day)

2 13 28 12 * /bin/execute/this/script.sh

A Job you want to run at 23:01 every friday

1 23 * * 5

What if you'd want to run something every 10 minutes? Well you could do this:

0,10,20,30,40,50 * * * * /bin/execute/this/script.sh

Alternative notation for the above is below

*/10 * * * * /bin/execute/this/script.sh

By default cron saves the output of /bin/execute/this/script.sh in the user's mailbox (root in this case).

But it's prettier if the output is saved in a separate logfile. Here's how:

*/10 * * * * /bin/execute/this/script.sh 2>&1 >> /var/log/script_output.log

By default cron saves the output in the user's mailbox (root in this case) on the local system. But you can also configure crontab to forward all output to a real email address by starting your crontab with the following line:

MAILTO="yourname@yourdomain.com"

NOTE: for permissions security you should have the script as read/execute (no write) only, with nobody as the owner or root only permissions

EXAMPLE CRONTAB FROM A LIVE SYSTEM

# min hour  calendarday month dayoftheweek   command
# last updated 2010-06 johnpfeiffer


50 19 * * * /trac-root/tracScripts/daily-backup-projects.sh >>/home/USER/daily-backup.log
59 19 * * * /trac-root/tracScripts/svnDUMP-backup.sh >>/home/USER/svnDUMP-backup.log

# every weekday at 8:59 every day of the month every month run the script send a report of the backups by email
59 7 * * 1-5 /trac-root/tracScripts/backupCHECKUP.sh


# every other weekday at 17:00 remove the old SQLBACK files so that tape Brightstor tape drive can finish
00 17 * * 2-4 /trac-root/tracscripts/SQLBACK-2day-deletion.sh

TROUBLESHOOTING

Cron running twice (duplicate output)

ps aux | grep cron

should show you if cron is running, try stopping the cron service, then editing, then starting it again

/etc/init.d/cron stop
crontab -e
/etc/init.d/cron start

  • « Linux temperature monitoring centos 5 3 hp proliant
  • make windows 32 add to system path »

Published

Jul 6, 2010

Category

linux

~384 words

Tags

  • cron 3
  • crontab 2
  • linux 249
  • list 23
  • schedule 2
  • scripts 63
  • user 18