Crontab overview

Overview

The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically.

Generally, crontab uses a daemon, crond, which runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. These jobs are generally referred to as cron jobs. Cron jobs run as the user who creates them, as though that user typed the command into their shell.

When creating the cron job in the panel, you are able to assign it to either an SFTP or shell user.

However, only shell users are able to manually create and edit cron jobs on the server.

Crontab file contents on the server

Due to the complex nature of translating user supplied content from the panel to the server, what you enter in to the panel is not what you see in the crontab file. For example:

The crontab file on the server then appears as follows:

###--- BEGIN DREAMHOST BLOCK
###--- Changes made to this part of the file WILL be destroyed!
# Cron Example
MAILTO="admin@example.com"
@hourly /usr/bin/setlock -n /tmp/cronlock.3788814087.215158 sh -c $'/usr/local/php82/bin/php /home/username/example.com/script.php'
###--- You can make changes below the next line and they will be preserved!
###--- END DREAMHOST BLOCK

When the 'Use locking' option is selected, the command(s) you want executed are filtered through a unique invocation of the setlock command.

View the setlock man page for further information. The use of setlock results in error messages if your cron job is invoked before the last iteration of the job releases your unique lockfile. This can happen if your script takes more time to run than the time between job executions.

Output

When does the cron job output data?

You will only receive an email of the cron job's output if the script it executes is written to output data to the terminal.

If the script the cron job executes does not create any output on the command line, there is no output to email.

Where is the output sent?

All output is emailed to the location specified in the MAILTO variable.

How does a script create output?

You can provide special instructions for the standard out STDOUT and standard error STDERR output by using the > operator.

  • When you use > without a number before it, it defaults to 1>. This is the standard (non-error) output.
  • When you use 2> you are specifying what to do with the error output.

For example:

  • >my_file.txt would redirect standard output to a file called my_file.txt
  • 2>my_errors.txt would redirect the errors to a file called my_errors.txt.

Permissions

By default, files created on DreamHost's servers have a permissions level of 644. If you choose to execute a script via cron, you may need to set the permissions for the file to 744 using chmod in order to allow it to execute properly.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?