Sunday, September 9, 2012

Crontab Scripts Redirection


### Crontab scripts Redirection ###

Crontab Log: How to Log the Output of My Cron Script
$ crontab -e
59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log 2>&1

In the above:

> /home/john/logs/backup.log indicates that the standard output of the backup.sh script will be redirected to the backup.log file.
2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1).
So, both standard output and error will be redirected to /home/john/logs/backup.log

---
/opt/testscript1
"50 * * * * /opt/testscript1 > /var/log/testscript1.log 2> &1
FREQUENCY OF JOBS
Every 50th Minute of every hour
Redirecting both STDOUT and STDERR to /var/log/testscript1.log

---
/opt/testscript2
"50 * * * * /opt/testscript2 > /var/log/testscript2.log 2>&1
FREQUENCY OF JOBS
Every 50th Minute of every hour 

---
30 0 * * * /opt/test3
Every 30th Minute of 0th HOUR

---
15 */6 * * * /opt/test4 > /dev/null 2>&1
Every 15th Minute on every alternate 6 hours

---
5 1 * * 2 /opt/test4 
Every 5th Minute on every 1st Hour of Tuesday 

---
15 */4 * * * /opt/test5 2> /dev/null
Every 15th Minute on every alternate 4 hours

---
19 12 * * * /opt/test6 > /dev/null 2>&1
Every 19th Minute on every 12th Hour

---
5 1 * * * find /opt -mtime +7 -exec rm -f {} \;
Every 5th Minute on every 1st Hour
This script deletes the files older than 7 days in /opt folder

---
36 1 * * * find /opt -type d -empty -exec rmdir {} \;
Every 36th Minute on every 1st HOUR
This script deletes the directories created in /opt

---
45 */1 * * * /opt 2>&1
Every 45th Minute on every alternate Hour

---
45 */2 * * * /opt/test >> /var/test.log 2>&1
Every 45th Minute on every alternate 2 hours




No comments: