Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

#!/bin/bash

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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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