Linux Commands Examples

A great documentation place for Linux commands

cron

daemon to execute scheduled commands (Vixie Cron)


see also : crontab - run-parts

Synopsis

cron [-f] [-l] [-L loglevel]


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 can I view results of my cron jobs?

The tasks cron is executing are responsible for their own logging.

0
source

cron: run a process but only if it isnt running?

You could use a lock file in your script, but please see Process Management.

flock is one utility that can be used.

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

Run Linux script at time 'n' on first day, 'n+15 mins' next day, and so on

You can do this via cron.

You call the same cron script every day at the same time, but you add a sleep statement to the start of the script that sleeps for 15m * date +'%j', this day of the year, [ 001 ... 366 ].

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 can I permanently fix my date synchronize problem in linux?

I had the same problem yesterday but under Slackware 8. To make a long story short, I read a lot on google to finally reimage the computer. My manager sent me that link but it did not fixed my issue.

I changed the local to UTC time. I changed the timezome to have the good one I ln both.

Hope this will help you!

Also, you can try this:

ntpdate 0.pool.ntp.org
hwclock --systohc

0
source

what is run-parts in /etc/crontab, and how do I use it.

Basically, run-parts(8) takes a directory as an argument.

It will run every script that is found in this directory. For example, if you do a listing of /etc/cron.hourly, you'll see that it's a directory where you can put executable files to be run every hour.

As you can see, in cron it's used for convenience, since you only have to specify one directory and everything in that directory will be executed. This makes it easy to maintain scripts in one of the etc/cron* directories.

See its manpage for more options that could be exploited for your own use cases. You could for example do a simple check and show which scripts would be run:

run-parts -v –test /etc/cron.hourly

0
source

Root account locked/suspended?

Okay, I've learned a bit about PAM and have found a solution.

Please note that this is not good practice in the general case, but in the context of reliable embedded systems I believe it to be acceptable.

My solution was to edit the cron specific file /etc/pam.d/cron like so:

-@include common-account  
+CONTENTS_OF_COMMON_ACCOUNT_FILE

and modify the following included line like so:

-account    [success=1 new_authtok_reqd=done default=ignore]  pam_unix.so  
+account    [success=1 new_authtok_reqd=1 default=ignore]     pam_unix.so

Cron jobs should now run even if the user's password needs to be changed.

0
source

Auto suspend after X minutes to stop myself procrastinating

I found this script for a suspend timer with warnings. If you would run this as a cronjob, all you would have to do for snoozing is creating a launcher (or whatever you wm calls it, a link on the desktop, taskbar, wherever you want it) that restarts the current job (with 30 min timer) and execute the script once with a two minute timer.

0
source

linux: how to run a command in a given directory

Your question makes little sense. Use cd. If you ask for the answer not to use cd because you've tried it and it didn't work, it's likely you didn't enter it correctly in the crontab.

The simplest way would be to write a small shell script that will cd and run your main task, then cron the script.

0
source

Verify that a cron job has completed

/var/log/cron

you can check to if its currently running with:

ps aux

0
source

Cron job once a minute for one hour a day

Sure:

*/1 4 * * * /home/scripts/my_job.sh

That tells it to run every minute during the 4 AM hour.

0
source

Problem with $RANDOM in crontab

cron typically runs with a less "full" environment, meaning that you simply don't have many of the same environment variables available to you. Apparently $RANDOM is one such, and in fact your sleep command is simply failing with an error because of the undefined variable -- which is why your command failed to run at all when you switched to && instead of ;. (Well, actually, $RANDOM is a Bash function, but cron doesn't run in a full Bash environment, which evidently lacks this function.)

To accomplish this task, you will need to use a separate Bash script, as you said. Alternatively, you might be able to figure out a way to use cat /dev/urandom directly in the cron command, but it would probably be easier just to move what you currently have to a separate Bash script.

description

cron is started automatically from /etc/init.d on entering multi-user runlevels.

options

-f

Stay in foreground mode, don’t daemonize.

-l

Enable LSB compliant names for /etc/cron.d files. This setting, however, does not affect the parsing of files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly.

-L loglevel

Tell cron what to log about jobs (errors are logged regardless of this value) as the sum of the following values:

1

will log the start of all cron jobs

2

will log the end of all cron jobs

4

will log all failed jobs (exit status != 0)

8

will log the process number of all cron jobs

The default is to log the start of all jobs (1). Logging will be disabled if levels is set to zero (0). A value of fifteen (15) will select all options.

debian specific

Debian introduces some changes to cron that were not originally available upstream. The most significant changes introduced are:

Support for /etc/cron.{hourly,daily,weekly,monthly} via /etc/crontab,

Support for /etc/cron.d (drop-in dir for package crontabs),

PAM support,

SELinux support,

auditlog support,

DST and other time-related changes/fixes,

SGID crontab(1) instead of SUID root,

Debian-specific file locations and commands,

Debian-specific configuration (/etc/default/cron),

numerous other smaller features and fixes.

Support for /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly is provided in Debian through the default setting of the /etc/crontab file (see the system-wide example in crontab(5)). The default sytem-wide crontab contains four tasks: run every hour, every day, every week and every month. Each of these tasks will execute run-parts providing each one of the directories as an argument. These tasks are disabled if anacron is installed (except for the hourly task) to prevent conflicts between both daemons.

As described above, the files under these directories have to be pass some sanity checks including the following: be executable, be owned by root, not be writable by group or other and, if symlinks, point to files owned by root. Additionally, the file names must conform to the filename requirements of run-parts: they must be entirely made up of letters, digits and can only contain the special signs underscores (’_’) and hyphens (’-’). Any file that does not conform to these requirements will not be executed by run-parts. For example, any file containing dots will be ignored. This is done to prevent cron from running any of the files that are left by the Debian package management system when handling files in /etc/cron.d/ as configuration files (i.e. files ending in .dpkg-dist, .dpkg-orig, and .dpkg-new).

This feature can be used by system administrators and packages to include tasks that will be run at defined intervals. Files created by packages in these directories should be named after the package that supplies them.

Support for /etc/cron.d is included in the cron daemon itself, which handles this location as the system-wide crontab spool. This directory can contain any file defining tasks following the format used in /etc/crontab, i.e. unlike the user cron spool, these files must provide the username to run the task as in the task definition.

Files in this directory have to be owned by root, do not need to be executable (they are configuration files, just like /etc/crontab) and must conform to the same naming convention as used by run-parts(8): they must consist solely of upper- and lower-case letters, digits, underscores, and hyphens. This means that they cannot contain any dots. If the -l option is specified to cron (this option can be setup through /etc/default/cron, see below), then they must conform to the LSB namespace specification, exactly as in the --lsbsysinit option in run-parts.

The intended purpose of this feature is to allow packages that require finer control of their scheduling than the /etc/cron.{hourly,daily,weekly,monthly} directories to add a crontab file to /etc/cron.d. Such files should be named after the package that supplies them.

Also, the default configuration of cron is controlled by /etc/default/cron which is read by the init.d script that launches the cron daemon. This file determines whether cron will read the system’s environment variables and makes it possible to add additional options to the cron program before it is executed, either to configure its logging or to define how it will treat the files under /etc/cron.d.

environment

If configured in /etc/default/cron in Debian systems, the cron daemon localisation settings environment can be managed through the use of /etc/environment or through the use of /etc/default/locale with values from the latter overriding values from the former. These files are read and they will be used to setup the LANG, LC_ALL, and LC_CTYPE environment variables. These variables are then used to set the charset of mails, which defaults to ’C’.

This does NOT affect the environment of tasks running under cron. For more information on how to modify the environment of tasks, consult crontab(5)

The daemon will use, if present, the definition from /etc/timezone for the timezone.

The environment can be redefined in user’s crontab definitions but cron will only handle tasks in a single timezone.

notes

cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them.

cron also reads /etc/crontab, which is in a slightly different format (see crontab(5)). In Debian, the content of /etc/crontab is predefined to run programs under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. This configuration is specific to Debian, see the note under DEBIAN SPECIFIC below. in order

Additionally, in Debian, cron reads the files in the /etc/cron.d directory. cron treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. This change is specific to Debian see the note under DEBIAN SPECIFIC below.

Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.

/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. The files under /etc/cron.d do not need to be executable, while the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly do, as they are run by run-parts (see run-parts(8) for more information).

cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists). The children copies of cron running these processes have their name coerced to uppercase, as will be seen in the syslog and ps output.

Additionally, cron checks each minute to see if its spool directory’s modtime (or the modtime on the /etc/crontab file) has changed, and if it has, cron will then examine the modtime on all crontabs files and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified. Note that the crontab(1) command updates the modtime of the spool directory whenever it changes a crontab.

Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of daylight savings time. If the time has moved forwards, those jobs which would have run in the time that was skipped will be run soon after the change. Conversely, if the time has moved backwards by less than 3 hours, those jobs that fall into the repeated time will not be re-run.

Only jobs that run at a particular time (not specified as @hourly, nor with ’*’ in the hour or minute specifier) are affected. Jobs which are specified with wildcards are run based on the new time immediately.

Clock changes of more than 3 hours are considered to be corrections to the clock, and the new time is used immediately.

cron logs its action to the syslog facility ’cron’, and logging may be controlled using the standard syslogd(8) facility.


see also

crontab , crontab, run-parts


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