1

I want to have a certain script (rails rake task) running on my server at all times - it basically populates my DB using a live feed. I can create a screen (http://linux.die.net/man/1/screen) and run the task easily, but I'd like to automate it in case it fails or stops (whenever I deploy to production, it stops).

I've created a script that does this perfectly:

# my_script

#!/bin/bash

start_myscreen () {
    screen -dmS myscreen bash -c 'cd /var/app/current/; rake my_task RAILS_ENV=production'
}

if screen -list | grep -q "No Sockets found"; then
  echo "no screens available"
  echo "starting myscreen"
  start_myscreen
else
  echo "screens available!"
  if screen -list | grep -q "myscreen"; then
    echo "myscreen exists - restarting"
    screen -X -S myscreen quit  
  else
    echo "myscreen does NOT exist - creating now"
  fi
  start_myscreen
  echo "Finished!"
fi

It is designed so that no matter what, the screen "myscreen" should be initiated with the rake task started by the time the script finishes. Obviously in production I won't be running the script every minute, this is currently only for testing. This script works perfectly when executed from terminal via:

./my-script

The problem occurs when I run it using this very simple crontab:

* * * * * ./my-script

What is strange is that the script is outputting everything correctly (based on the message sent to me by mail from crontab), but I do not see any screens running after the cron executes when I run:

screen -list

Very strange.

BTW - the server runs 64bit Amazon Linux (red-hat I believe)

Thanks for any help.

1 Answer 1

1

First, note that the current directory in a crontab is your home directory. You should put the full path to the script in the crontab. However, since your script is being executed, this isn't the problem.

The likely problem is that your script requires an environment variable that is set in your normal session. Cron jobs run with a minimal environment, your .profile is not loaded.

On many systems, ~/.pam_environment is read even for cron jobs. Note that this is not a shell script, ~/.pam_environment only supports a restricted syntax.

If you can't use .pam_environment, you can have .profile read explicitly:

 * * * * * . ~/.profile && ./my-script

You aren't seeing any errors because they're emitted inside the screen window and screen closes immediately afterwards. Add the -L option to your screen invocation, and preferably a logfile directive to your .screenrc, to make screen log the output from rake, in case this kind of things happens.

1
  • Hey, thanks for the response. Glad you mentioned the logging option -L, that showed me that it was actually just a bundler issue in my environment. I cd'ed into my app's directory on my ec2 (/var/app/current) and ran "sudo path/to/bundle install" - that fixed it. To anyone with this issue: make sure you run "which bundle" to get the full path to your bundle command. May 20, 2015 at 14:04

You must log in to answer this question.

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