crontab running as a specific user

Only /etc/crontab and the files in /etc/cron.d/ have a username field. In that file you can do this:

1 1 * * * username /path/to/your/script.sh

From root's crontab sudo crontab -e you can use:

1 1 * * * su username -c "/path/to/your/script.sh"

Or you can use the user's actual crontab like this:

sudo crontab -u username -e

The second column in any crontab file is for the hour that you want the job to run at. Did you mean the sixth field?


You may need to set the PATH for that user, if the executable you're using is in, say, /usr/local/bin. To set the PATH, put something like this before the cronjobs:

PATH=/bin:/usr/bin:/usr/local/bin

*/5 * * * * user1 sample_executable

The other way is to fully specify the path to sample_executable in your cronjob, so:

*/5 * * * * user1 /path/to/sample_executable

If sample_executable refers the executables not in the standard PATH, you should use the first option.


The typical directory for user crontabs is in /var/spool/cron/crontabs. The file format is the one that doesn't include the username. User crontabs are owned by the user and named after the user with mode 0600. This is best handled by writing your tab file and using crontab -u username filename to setup whatever cron entries you want for that user.