crontab quick reference
=====================
crontab entries
crontab entries are stored in a file: /var/spool/cron/$USER
The actual path may very per Linux distribution.
For example:
root@my_host:/var/spool/cron>% ls -l
-rw------- 1 oracle root 77 May 17 2011 oracle
-rw------- 1 root root 236 Jun 27 2012 root
-rw------- 1 shdaemon root 424 Mar 5 2014 shdaemon
The crontab files are owned by root.
The proper way to access these files is via crontab CLI.
crontab -l - view
crontab -e - edit
Entry example:
00 05 * * * . $HOME/.profile_for_awr;
00 - min. 00-59
05 - hour. 00-23
* - Day of month. 1-31
* - Month. 1-12
* - Day of week (0 - 6) (Sunday=0)
crontab log
/var/log/cron
for example:
Jul 23 04:04:01 my_server crond[2167]: (root) CMD (/usr/local/etc/my_user/load_alarm/loadalarmer > /dev/null 2>&1)
Jul 23 04:04:01 my_server crond[2169]: (root) CMD (/usr/local/etc/my_user/load_stats/bin/load_stats)
Jul 23 04:05:01 my_server crond[3720]: (root) CMD (/usr/local/etc/my_user/load_stats/bin/load_stats)
Jul 23 04:05:01 my_server crond[3721]: (root) CMD (/usr/local/etc/my_user/load_alarm/loadalarmer > /dev/null 2>&1)
Jul 23 04:06:01 my_server crond[5295]: (root) CMD (/usr/local/etc/my_user/load_stats/bin/load_stats)
Jul 23 04:06:01 my_server crond[5296]: (root) CMD (/usr/local/etc/my_user/load_alarm/loadalarmer > /dev/null 2>&1)
=======================
Examples:
=======================
# To run at 00:00, 08:00, 16:00
00 0,8,16, * * * /run/something.sh
=======================
Example 1.
=======================
Remove multiple files, older than 5 days with one command from crontab
Run on 23:15 every day.
# To run at 00:00, 08:00, 16:00
00 0,8,16, * * * /run/something.sh
=======================
Example 1.
=======================
Remove multiple files, older than 5 days with one command from crontab
Run on 23:15 every day.
15 23 * * * find . -type f -name "FILE-TO-FIND" -mtime +5 -exec rm {} \;
=======================
Example 2.
=======================
Delete *.trc and *.trm files older than 7 days, run every day at 04:00.
crontab -l
0 4 * * * /usr/local/etc/userA/oracle_purge/delete_old_trace_files.sh
less /usr/local/etc/userA/oracle_purge/delete_old_trace_files.sh
#!/bin/bash
export ORA_INST=orainst
export DAYS_TO_KEEP=2
find /software/oracle/diag/rdbms/${ORA_INST}/${ORA_INST}/trace -type f -name "*.trc" -mtime +${DAYS_TO_KEEP} -exec rm {} \;
find /software/oracle/diag/rdbms/${ORA_INST}/${ORA_INST}/trace -type f -name "*.trm" -mtime +${DAYS_TO_KEEP} -exec rm {} \;
=======================
Example 3.
=======================
Keep only last hour of listener.log file.
crontab -l
1 * * * * /software/oracle/oracle/scripts/clean_files.sh
less /software/oracle/oracle/scripts/clean_files.sh
#!/bin/bash
#!/bin/bash
MY_SERVER=zaf-vod-7-aps-1
MY_INST=igt
mv -f /software/oracle/diag/tnslsnr/${MY_SERVER}/lsnr_${MY_INST}/trace/lsnr_${MY_INST}.log /software/oracle/diag/tnslsnr/${MY_SERVER}/lsnr_${MY_INST}/trace/lsnr_${MY_INST}_last_hour.log
==================================
crontab log
==================================
under /var/log/
cron file.
older files are cron.1, crom2, etc.
==================================
under /var/log/
cron file.
older files are cron.1, crom2, etc.
==================================
Example of crontab task to run sql
==================================
Just put this task inside oracle crontab
5 01,13 * * * . /etc/profile ; echo "TRUNCATE TABLE MY_SCHEMA.MY_TABLE;" | sqlplus /Just put this task inside oracle crontab
==================================
crontab and environment variables
==================================
cron is started by the system, so it has a minimal environment.
It knows nothing about your shell and the local shell environment variables.
Thus need to set the environment variables before running the script, by:
. $HOME/.set_env;
Note the ";" at the end of the source command.
Which variables need to be set?
PATH
LD_LIBRARY_PATH
For example :
env | grep PATH > .set_env
and inside the main script:
. .set_env
==================================
This will not work in cron:
write to ./my_file
This will work:
write to /usr/my_user/my_path/my_file.txt
. $HOME/.set_env;
Note the ";" at the end of the source command.
or to source . $HOME/.set_env from inside the shell script.
Which variables need to be set?
PATH
LD_LIBRARY_PATH
For example :
env | grep PATH > .set_env
and inside the main script:
. .set_env
==================================
crontab and absolute path vs relative path
==================================
In cron - ALWAYS use absolute path.This will not work in cron:
write to ./my_file
This will work:
write to /usr/my_user/my_path/my_file.txt
==================================
Checklist for crontab task is not running.
==================================
A. Is crond service working?
Run
B. is cron working?
* * * * * /bin/echo "cron works" >> /path/test_cron.log
C. Is the script working?
Try to run the job from $HOME
D. Check cron log for possible errors
Check
ps ax | grep cron
and look for crond process.B. is cron working?
* * * * * /bin/echo "cron works" >> /path/test_cron.log
C. Is the script working?
Try to run the job from $HOME
D. Check cron log for possible errors
Check
/var/log/cron.log
or /var/log/messages
E. Check the Environment variables from inside the script
just put:
echo "PATH = " $PATH >>/path/test_cron.log
echo "ORACLE_HOME = " $ORACLE_HOME >>/path/test_cron.log
=======================
No comments:
Post a Comment