Linux – From what context is a cron task executed

bashcroncrontabredhat-enterprise-linux

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.

Best Answer

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.

Related Question