Search code examples
linuxcroncasperjs

Running a casper.js script from cron


I'm trying to run a casper.js script via cron. Everything works fine when I run the script manually, but when I run it via cron I get the following errors:

Traceback (most recent call last):
 File "/usr/local/bin/casperjs", line 46, in <module>
   status = subprocess.call(CASPER_COMMAND)
 File "/usr/lib/python2.6/subprocess.py", line 480, in call
   return Popen(*popenargs, **kwargs).wait()
 File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
   errread, errwrite)
 File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
   raise child_exception
OSError: [Errno 2] No such file or directory

My crontab entry is:

30 9 * * * /usr/local/bin/casperjs lib/fsaupload.js arg1 arg2 arg3

I've also tried

30 9 * * * python /usr/local/bin/casperjs lib/fsaupload.js arg1 arg2 arg3

Which gives me the same result. Any ideas? I'm guessing it might be a path issue, but no idea where to go from here!


Solution

  • You should probably use an absolute path to your casper script, something like:

    30 9 * * * /usr/local/bin/casperjs /absolute/path/to/lib/fsaupload.js arg1 arg2 arg3
    

    My two cents.

    Edit:

    Okay, it was a bit silly. You can also set a custom path to the phantomjs executable by setting the PHANTOMJS_EXECUTABLE environment variable:

    $ export PHANTOMJS_EXECUTABLE="/path/to/phantomjs"
    

    Then run your script as usual:

    /usr/local/bin/casperjs /absolute/path/to/lib/fsaupload.js arg1 arg2 arg3
    

    Hint: If your crontab runs as another user, check that it has access to the phantomjs path.

    Hope it helps (and works).

    Edit again

    Wait, the stack trace you get says you're using an old version of CasperJS (eg. the subprocess module is no more used). Try with a more recent version :)