The cron daemon was designed in such a way that it does NOT execute commands within your normal shell environment. This means you cannot use bare commands in cron the way you would from the SSH shell command line. This is because the PATH environment variable is /usr/bin:/bin, and the SHELL environment variable is set to /bin/sh. This is an example using the 'wp' command.
* * * * * wp plugin update --all --path=example.com
The cron daemon doesn't know where the 'wp' command is located. You must specify the full path to all commands not in the default PATH, and always specify the full path for files. For example:
* * * * * /usr/bin/wp plugin update --all --path=example.com
If you have any trouble locating where a command is, you can use the following command:
[server]$ which command-name
[server]$ which wp
The following prints to your terminal:
Using sudo and cron with an admin user on a Dedicated server
If you’re on a Dedicated Server and have an admin user, do not use 'sudo' in your crontab. Instead of your script working, you’ll get an error like this.
[server]$ sudo: no tty present and no askpass program specified
This is because 'sudo' requires a password to be entered in order to run and cron jobs (along with any program/command/script you could execute from them) do not allow any input to be specified.
If you need root level permissions for a script, you’ll need to create a file in crontab format in the /etc/cron.d/ directory. Of course, make sure to keep a backup of that file as that location is outside the /home directory and not included in DreamHost’s internal backups.
MAILTO variable requirement
For performance reasons, DreamHost now requires all crontabs to have a MAILTO variable set.
The MAILTO variable can be set to anything. If you leave it blank, it does not email output of your cronjobs. You can set it to your username to have it delivered to your user as usual, or you can specify an external email address. Please note that if you specify an external email address, any cron emails that get sent (including completion/error notification messages) factor in to your shell user's hourly email sending quota. For more information about that limit, please view the SMTP quota article.
If your cron job doesn't create any output on the command line, no email is sent even if the cron job is set to send email.
Examples of a valid MAILTO configuration in a crontab file
To disable sending cronjob output via email:
To send cronjob output to firstname.lastname@example.org:
To send cronjob output to your shell user’s Maildir folder:
As stated when running
man 5 crontab, strings are not parsed for environmental substitutions or replacement of variables. This means that MAILTO=$LOGNAME does not work.
Cron job character limit
When adding/modifying a cron job in the panel, the 'Command to run' field specifies a maximum of 1000 characters. This limit does not include the additional content DreamHost adds to the cron job (such as locking or the sh -c $’’ encapsulation). On average, that additional content should not be more than 100 characters. Violating the limit results in this warning:
/bin/sh: -c: line 1: unexpected EOF while looking for matching `'' /bin/sh: -c: line 2: syntax error: unexpected end of file
If you encounter this error it’s recommended that you encapsulate your commands into a script and modify the cron job to execute the script instead. For example, if you place the command (or commands) that contain more than 1000 characters in a file named ‘cron.sh’ and place that file in your user’s home directory, you could then set the cron job command as follows.
[server]$ . ~/cron.sh
Be sure to review the Execution environment section above to make sure you account for any missing variables, paths, and/or declarations.