Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to run a cron job as a specific user on my machine. How may I specify the user for a cron job to run as?.

I am running on Ubuntu 10.0.4


The cron jobs will be running on a server. The 'users' are users that have been created specifically for carrying out specific server side tasks. These 'users' have the following in common:

  • Cannot log onto the system.
  • Have restricted access to specific folders/files.
share|improve this question
up vote 50 down vote accepted

Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.


#<timing>   <user> <command>
11 * * * *  root   /usr/lib/command
share|improve this answer
I think this is the correct answer. I will have to look for some examples and investigate some more. – morpheous Aug 2 '10 at 12:19
Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d). – Patkos Csaba Aug 2 '10 at 12:25

As root, to edit the cron of user1:

crontab -u user1 -e

You can also start your command with:

su user1 -c foo bar

But often, the scripts themselves reduce their own access when started as root.

share|improve this answer

I have been looking for this for a couple weeks and this finally worked...

Create your script as user1

ssh user1@ipaddress
nano hourly-event

enter some command

echo YAY it works > /home/user1/yay.txt

make it executable

chmod 755 hourly-event

edit the crontab for user1

sudo crontab -u user1 -e

put a line at the bottom pointing to your script(s)

# m h  dom mon dow   command
00  *   *   *   *    bash ./hourly-recording

exit - saving changes (it will show a /tmp directory when saving... it's ok)

wait for the turn of the minute

open your newly create yay.txt

nano /home/user1/yay.txt

you should now have a nano window open with "YAY it works" as the first and only line if the bottom of your nano window say New File... well.. i dunno


share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .