Dev
Dev

Reputation: 365

Cron not executing shell script .. but working from commandline

My os is Ubuntu 12.04

I am trying to execute a shell script from cron tab ..

that shell script works fine .. when i am executing directly in command line .. like

sh out.sh

it works fine .. but when i am set cron for this shell script its not working

my shell script : out.sh:

#!/bin/bash
firefox "http://localhost/acceptance/selenium-main/shell.php"

it will opens that shell.php webpage in firefox browser ... its works fine when executing directly from CLI

..i an setting cron job like this

sudo crontab -e

then

23 13 * * * bash /usr/share/nginx/www/acceptance/selenium-main/out.sh

this was not working ..

i even tried

33 13 * * * /usr/share/nginx/www/acceptance/selenium-main/out.sh

this was also not working ..

even i tired executing from bin also : /usr/local/bin/out.sh

none of the methods are working

Please suggest how to fix this .. because crontab not executing shell scripts ..

Upvotes: 2

Views: 4407

Answers (5)

Bohemian
Bohemian

Reputation: 424983

The big difference between running something in cron and from the command line is that your profile is run for your shell, so all the environment variables needed by your script are set correctly, but cron uses a naked "profile-less" shell.

Try using a script that sets all the environment variables it needs before executing the whatever the script does, so its "self contained".

Upvotes: 0

anthonyc
anthonyc

Reputation: 187

How about

php /path_to_localhost/acceptance/selenium-main/shell.php

If it is just producing a report and has no user interations, just run the script. You do not need a browser to run php scripts.

Upvotes: 0

VoidPointer
VoidPointer

Reputation: 3097

It looks like the behaviour of cron when configuring GUI applications. Try creating a wrapper script like,

wrapper_script:

export DISPLAY=:0.0
xhost + 2>>/tmp/err.log
firefox "http://localhost/acceptance/selenium-main/shell.php" 2>>/tmp/err.log

Add cron entry as,

33 13 * * * bash /path/to/wrapper_script.sh

Upvotes: 0

Ben Matheja
Ben Matheja

Reputation: 115

try the same solution konsolebox added to the firefox line in your shell script. maybe firefox is not in your path

Upvotes: 0

konsolebox
konsolebox

Reputation: 75478

Try to use absolute path:

23 13 * * * /bin/bash /usr/share/nginx/www/acceptance/selenium-main/out.sh

Also try to run bash in login mode if something doesn't work right.

23 13 * * * /bin/bash -l /usr/share/nginx/www/acceptance/selenium-main/out.sh

That would try to fix other variables like PATH. If not, try to explicitly set it in your script, or just use absolute paths everywhere.

Upvotes: 2

Related Questions