Linux crontab example: How to run a program as another user

Assuming you are the root user on a Linux computer, here's a quick example of how to run a program with a Linux crontab entry as another user. Specifically in this example, the program being executed is run as the user nobody.

To run a program as another user from crontab, just put an entry similar to the the following in a Linux or Unix crontab file (typically by issuing the "crontab -e" command), and the program named will be run at 1:30 a.m. using the Bourne shell, and will be run as the user nobody.

30 1 * * * su -c '/path/to/program/' -s /bin/sh nobody

You can verify that your program is run by the user you specify by creating a simple test program that prints the user environment, either to a file, or that is sent in an email, and run that program from a crontab entry like this.

note that although this crontab example demonstrates how to run a script as the Linux user nobody, I think you can see that the same crontab technique can be used to run a program as any Linux or Unix user by replacing "nobody" with that user's name.

For more information on the Linux crontab command, here's a link to an online copy of the Linux crontab man page.