how to run cron jobs as a specific user other than root in linux

Cron jobs are an essential part of Linux and Unix systems. The Cron is a software utility that is available on almost all versions of Unix and Linux by default. It is a time-based scheduler program that can run jobs, such as commands and scripts at specified days or times. As mentioned it is primarily used for system maintenance purposes but you could use it for any purpose.

As you might already know, Cron jobs are maintained in a configuration file. You can edit the configuration file by using the -e option in the crontab command. The -l option will display or list the currently configured jobs for the user.

$ crontab -e

You can usually run cron jobs as root without any issues. Most system maintenance jobs will need to be run root or super user anyways. However it is possible for each user in the system to have their own crontab or cron jobs. The system administrator or the super-user will need to provide the user with explicit permissions in order to run cron.

Crontab Permissions

There are two files that control the permissions for crontab: /etc/cron.allow and /etc/cron.deny. If there is a cron.allow file, then the user or users that need to use cron will need to be listed in the file. You can use cron.deny to explicitly disallow certain users from using cron.

If neither files exist, then only the super user is allowed to run cron. Well, that depends on the system specific configuration to be exact. Most configuration do not allow any users to run jobs, while some systems allow all users to run jobs by default.

So, the first step is to create a file named cron.allow in the /etc/ folder. Add the user name to this file in order to allow the user to run jobs.

Setting up Cron Jobs

Once the proper permissions has been set, the user should be able to modify and run jobs using the crontab command. The -e option allows the user to edit and add new jobs while the -l command line option can be used to list the jobs for that particular user.

$ crontab -l

I am going to assume that you are familiar with the cron job format and how to set up jobs. So, I am not going to go into the syntax of the cron job formats.

Cron Jobs as Other Users

If you are the super user then you can also modify or create the cron jobs of other users. The crontab command line option -u allows you to specify an username and edit the jobs of that user. To modify the cron jobs for user tom, use the following command.

$ crontab -u tom -e

The above allows you to modify the cron jobs for another user. But sometimes, you want to run a particular command as another user while still using the root or super-user crontab. You can use the su or sudo command to do that. While creating the job prefix the command with sudo -u or su <username> -c .

1 2 * * * su username -c "/path/to/my/scriptfile.sh"
1 2 * * * sudo -u username "/path/to/my/scriptfile.sh"

Troubleshooting

Existing systems can have varying types of issues, mostly related to file permissions. Make sure you have permissions to /var/spool/cron files. Most times the command output will let you know which file permissions are needed.

Execute the command that you want to run as a job from the command prompt. This will let you figure out what the error is most of the time.