I am asking this question after royally destroying a Linux box. Let me give you some background so you know where I am coming from.

I currently have (well... did have) a RedHat Linux box running Apache with Phusion Passenger added for running Ruby Apps under Apache. The app acts as a service to serve cached files to Kiosks in the Cincinnati area on a regular basis. The web app worked great and the only part left was to set up a cron task to retrieve all the information that needed to be cached and package that into a few different update packages for serving to the kiosks. To accomplish this I wrote a Ruby script that would handle the fetching and packaging of data. To setup the cron task, I just used the built-in crontab. The script looked like so:


cd ../lib
ruby pdf_cache.rb
ruby pdf_prepare.rb
ruby rss_cache.rb
ruby nightly-pack.rb 
cd ..
chown -R www.www *

This script was located in the cron folder and the directory structure looked like so:

|-- cron
|-- feeds
|-- lib
|   `-- trash
|-- logs
|-- nightly-packs
|-- pdf
|   `-- tank
|-- public
`-- tmp

and I ran crontab -e as root with the following contents:

0 3 * * * /usr/local/apache2/ruby_projects/kiosk/cron/schedule_job

Which should execute the script every day at 3am. The next day after scheduling this cron task, I got a very unfortunate surprise. The cron task was executed in the root and I assume that most of the commands failed, with the exception of chmod -R www.www * which changed ownership of everything in the system to the www user.

My question is; what is the proper way to implement a cron-task and from what context is the cron-task run from? I realize now that I probably should not be running this under the root account, and I am not sure how to run it under the www account as that user has no shell and you can't successfully su to the www user.

up vote 2 down vote accepted

I bet you could just give the user a shell, just limit down the ability to access it remotely and it's permissions.

Cron tasks run under the users who made the Cron task, so that's why it runs as root.

  • And does the script execute from the user's home directory or from the Linux root (/). I'm just curious why my entire box is now owned by the www user? Thanks! – John Apr 11 '11 at 22:58
  • From the user's home directory. As you made that Cron task under your root user, it landed in the root, and it did chmod -R www.www * so that's why the entire box is now owned by the www user... – Tom Wijsman Apr 11 '11 at 23:10

Another suggestion: add -e to the shebang line on scripts like this (i.e. #!/bin/bash -e). This'll make the script exit if any command in it fails. It doesn't guarantee you won't have any unpleasant surprises, but it will keep the script from running completely off the rails (so to speak), and prevent many kinds of chain reaction failures.

The proper way to do this is 'su - ', setup your cronjob as this users. Then it will run as this user. Or, depending on what cron daemon you are using, it might have a 6th field to define the user.

Hope this helps. There are other hacks, but you are always best served by making it work properly.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.