Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, June 5, 2013

Crontab

I can never seem to remember how to use crontab. I found a good resource for it that has lots of examples and good explanation of things. Check it out. This is also a good one.

The highlights that I want to remember are:

crontab -l
This is used to see what is scheduled to be executed and when.

crontab -e
Opens the vi text editor to allow changes to the crontab. In most implementations that I have seen the changes are automatically picked up by crontab
vi remember:
  • ESCAPE then ZZ to save changes
  • ESCAPE then :q to quit and not save changes
  • ESCAPE then i to go into edit mode
Each line in the crontab has the format:
min hr dom month dow cmd

  • minute This controls what minute of the hour the command will run on, and is between '0' and '59'
  • hour This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight) 
  • dom This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19.
  • month This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
  • dow This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun). 
  • user This is the user who runs the command. 
  • cmd This is the command that you want run. This field may contain multiple words or spaces
Some examples:
30 7 * * * /opt/tomcat/myApp/run.sh "This command is run daily at 7:30 am"
0 * * * * echo "This command is run hourly"

Special Keywords that can be used instead of the 5 part specifiers
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.
For example:
@yearly /home/ramesh/red-hat/bin/annual-maintenance









Monday, June 11, 2012

Find file by arbitrary date in Linux

To find all files that are newer than an arbitrary date you can use the example below. To make this work, we have to use a little trick. First we create an empty file with last modified date that matches the date we are interested in using in our search. We write it to our user directory since we should have write permissions to it in most cases. Next we use the find command to find all files (starting in the current directory) newer than the file we created. When we are done, all we have to do is delete the file we created (or leave it if you want to use it later). It seems more difficult that it should be, but it does appear to work.

touch -d "06/11/12 13:37" ~/date_marker

find . –newer ~/date_marker

rm ~/date_marker