Linux Commands

How to Schedule Crontab Job to Run as a Specific User

By default, crontab schedules jobs for the current user. However, there are times when as an administrator, you may need to schedule jobs for other users. The good news is that crontab allows so when you add the -u option followed by the user you wish to schedule the jobs for. Also, you may need administrator permission before scheduling jobs for other users.

Today’s post covers how to schedule various jobs for different users as an administrator.

How to Schedule Jobs in Linux

Linux offers the cron utility, which allows users to schedule jobs by opening the crontab file using an editor. The command below opens a crontab file for the current user. If you need to schedule a job, you add the cron job at the bottom of the file.

crontab -e

The syntax for scheduling a job is:

MIN HOUR Day-of-Month MONTH Day-of-Week [command/script]

0-59   0-23        1-31               1-12             0-6

For instance, the command would be as shown below if you need to schedule a cron job that prints the current date and time for the logged user and stores it in a file at a given date and time.

25 19 * 7 * date > /home/kyle/date.txt

The scheduled job worked for the currently logged user.

You must use the -u <username> format to schedule a job for a specific user. The syntax for that would be:

sudo crontab -e -u username

For our case, we will use a user named linuxhint1.

The command will be shown below to open a crontab file for the user. Note that you must select which editor to use for the crontab file. We will use a nano editor for our example.

Once our crontab file for the specific user opens, we can schedule a job by creating it at the bottom of the file. In this case, we are creating a cron job that executes a script named user1.sh every first day of each month at 6:30 PM, as shown below.

Save the file and exit. You should get a message like the one below confirming the new crontab has been installed for the new user.

To list the scheduled job of a specific user, use the command below. Replace the username to match that of your target.

sudo crontab -l -u linuxhint1

In the output above, we can note the cron job we created for our specific user. If you don’t specify the user, you will get the scheduled jobs for the current user.

Similarly, you can remove the scheduled jobs of another user.

The command for removing scheduled jobs is:

crontab -r

However, that removes for the current user. You must add the username of the target user, as shown below.

sudo crontab -r -u linuxhint1

If we list the scheduled jobs for our user, linuxhint1, we see we have none. Thus, we successfully managed to remove the scheduled job.

sudo crontab -l -u linuxhint1

As an administrator, you have the power to schedule jobs for any user in the system. Specify their username and their crontab file will open for you to create a job. Then, specify the date and time it should execute.

Conclusion

Linux, Unix, and macOS make it easy to schedule jobs using the cron utility. This guide covered how to create jobs for a specific user. We discussed how you could create a job for another user, view the scheduled jobs, and delete the jobs provided you are an administrator.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.