Linux Commands Examples

A great documentation place for Linux commands

crontab

maintain crontab files for individual users (Vixie Cron)


see also : cron

Synopsis

crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r }


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

0
source

How to execute shell script via crontab?

4 hypothesis:

  • the cron daemon is not running (do a ps axfww | grep cron and check)

  • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.

  • your script is not executable

  • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path

0
source

Only email on cron errors for jobs in cron.daily, cron.hourly, etc

You should send successful email notifications to /dev/null so they disappear.

But you want to see unsuccessful email notifications.

This means you need to first direct stdout to /dev/null and then direct /dev/stderr to stdout

try changing the redirection part of your cronjobs to

>/dev/null 2>&1

See this link

0
source

Difference between /etc/crontab and "crontab -e"

One is the system crontab and can only be edited by root, and the other is the user crontab and can be edited by a user and exists per user.

0
source

From what context is a cron task executed?

I bet you could just give the user a shell, just limit down the ability to access it remotely and it's permissions.

Cron tasks run under the users who made the Cron task, so that's why it runs as root.

0
source

How do I make crontab generate a unique log each time it runs a job?

You can use the date command to generate a unique(ish) filename:

/usr/local/bin/python2.7 /home/scripts/script.py > /home/scripts/logs/"`date +\%d\%m\%y_\%H\%M\%S`".log 2>&1

EDIT: Make sure to escape the percentage signs (\%). They have a special meaning in cron and the crontab will return an error.

That way, a new log file (called <date_time>.log) will be created every time your job runs.

$ date; touch "`date +%d%m%y_%H%M%S`.log"
  Tue Sep 25 04:39:25 CEST 2012
$ ls *log
  250912_043925.log

Assuming your script does not run more often than once a second (which it can't if you are running a simple crontab), you should be fine.

If, as suggested in the comments, your script is run by different users, you can add the user name to the log file's name:

/usr/local/bin/python2.7 /home/scripts/script.py > /home/scripts/logs/$(echo -n `whoami`_`date +\%d\%m\%y_\%H\%M\%S`).log 2>&1

0
source

system-wide crontab vs. user crontab to shutdown pc

In theory either should work. I think you will find the difference is in the PATH. Your version may differ, but in my /etc/crontab there is a PATH line at the top which includes /sbin in it (and my shutdown is in /sbin).

I think you will find that when CRON is executed for the root user it does not have a path associated with it, so it is not working. Change the command to

30 2    * * 3   root    /sbin/shutdown -h now

And I suspect that will solve your problem.

0
source

How to create folders according to date in Linux?

Use the -p option to mkdir to create more than one element of the path at the time, and use a for-loop to avoid repeating yourself:

sh -c '
  for a in abcd wxyz; do 
    for b in dammi harrami; do 
      mkdir -p /home/$a/$b/$(date +%Y-%m)/$(date +%d); 
    done; 
  done
'

(line-breaks to show logical structure; could be all on one line)

0
source

Cron ignoring an update to crontab

Check the manpage on your system for crontab(5) (man 5 crontab), especially under the "EXAMPLE SYSTEM CRON FILE" section. Only the /etc/crontab file includes a username field. Non-system user crontabs do not include this field. If you want to edit /etc/crontab, then use a text editor, not the crontab(1) command.

0
source

How can I change the directory a command thinks it is running in?

Why does using cd in crontab scare you?

Note that if you want to just temporarily change directory and then come back, you can use pushd and popd

0
source

Do jobs in root's crontab still execute when logged in as another user?

Yes, root's cron jobs will still run even if you're logged in as another user.

0
source

crontabs hour specification */1 is that the same as *

Yes those are the same. From the man page (man 5 crontab):

A field may be an asterisk (*), which always stands for ``first-last''.
[...]
Ranges can include "steps", so "1-9/2" is the same as "1,3,5,7,9".

Therefore */x means the whole range (depending on the position of the *) covered with steps of x.

In your case, for the "hour" position, the first-to-last range is 0 to 23. So * = 0-23, and */1 = "0,1,2,...,22,23", which is exactly the same.

0
source

Why crontab command is not available to me even I am root

ls: cannot access /usr/bin/crontab: No such file or directory

I am able to see that file when ever I tried the locate command

In that case crontab is not installed anymore.

Locate does not search the filesystem. It looks in a database which is usually generated once per day (often around 3AM). If the the contab binary was present when locate last updated itself then it will show up with locate. Even if it got removed afterward.

In your case something deleted the that file, or uninstalled the package responsive for crontab.

0
source

Why cron saves at /root, when should at /var/www?

The querylog.log file is saved in the /root directory because that's exactly where you have specified it should be saved.

If you want to change its location, just edit the path to your desired location, e.g. /var/www/querylog.log.

0
source

Why are some of my cron jobs not sending deamon emails?

After you redirect the standard output of run.sh to /root/backup_scripts/new_scripts/$(date).run.log there is only the error output left to redirect to standard out. Could it be that run.sh just doesn't produce any errors?

If you want to see the standard output of run.sh use tee to save it to file, too.

45 03 * * * /root/backup_scripts/new_scripts/run.sh | tee "/root/backup_scripts/new_scripts/$(date).run.log" 2>&1

description

crontab is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.

If the /etc/cron.allow file exists, then you must be listed (one user per line) therein in order to be allowed to use this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command.

If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able to use this command.

If both files exist then /etc/cron.allow takes precedence. Which means that /etc/cron.deny is not considered and your user must be listed in /etc/cron.allow in order to be able to use the crontab.

Regardless of the existance of any of these files, the root administrative user is always allowed to setup a crontab. For standard Debian systems, all users may use this command.

If the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing). If this option is not given, crontab examines "your" crontab, i.e., the crontab of the person executing the command. Note that su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety’s sake.

The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename ’’-’’ is given.

The -l option causes the current crontab to be displayed on standard output. See the note under DEBIAN SPECIFIC below.

The -r option causes the current crontab to be removed.

The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically. If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.

The -i option modifies the -r option to prompt the user for a ’y/Y’ response before actually removing the crontab.

debian specific

The "out-of-the-box" behaviour for crontab -l is to display the three line "DO NOT EDIT THIS FILE" header that is placed at the beginning of the crontab when it is installed. The problem is that it makes the sequence

crontab -l | crontab -

non-idempotent -- you keep adding copies of the header. This causes pain to scripts that use sed to edit a crontab. Therefore, the default behaviour of the -l option has been changed to not output such header. You may obtain the original behaviour by setting the environment variable CRONTAB_NOHEADER to ’N’, which will cause the crontab -l command to emit the extraneous header.

diagnostics

A fairly informative usage message appears if you run it with a bad command line.

cron requires that each entry in a crontab end in a newline character. If the last entry in a crontab is missing the newline, cron will consider the crontab (at least partially) broken and refuse to install it.

files

/etc/cron.allow
/etc/cron.deny
/var/spool/cron/crontabs

There is one file for each user’s crontab under the /var/spool/cron/crontabs directory. Users are not allowed to edit the files under that directory directly to ensure that only users allowed by the system to run periodic tasks can add them, and only syntactically correct crontabs will be written there. This is enforced by having the directory writable only by the crontab group and configuring crontab command with the setgid bid set for that specific group.

standards

The crontab command conforms to IEEE Std1003.2-1992 (’’POSIX’’). This new command syntax differs from previous versions of Vixie Cron, as well as from the classic SVR3 syntax.


see also

cron tab, cron


author

Paul Vixie <paul[:at:]vix[:dot:]com> is the author of cron and original creator of this manual page. This page has also been modified for Debian by Steve Greenland, Javier Fernandez-Sanguino and Christian Kastner.

How can this site be more helpful to YOU ?


give  feedback