Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 636
Pezholio Avatar asked Jan 17 '23 14:01

Pezholio


2 Answers

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 :)

like image 181
NiKo Avatar answered Jan 25 '23 18:01

NiKo


This is an older question, but still relevant - I just spent 4 hours trying to resolve this, without finding a direct solution. What was happening for me was exactly the same, I could run the casper.js script from the shell command line, but not via a cron job. As alluded to by NiKo, but not spelled out clearly enough for me to get, casper needs to know where Phantomjs is in order to work.

I created a shell script, cron_wrap.sh, which contains the path to the Phantomjs bin directory:

#!/bin/bash
PATH=/usr/local/src/phantomjs/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
/usr/local/bin/casperjs --ignore-ssl-errors=true /srv/www/apps/myscript.js

Make your .sh executable, now you can add the script to cron:

00 09 * * * /srv/www/apps/myscript.js
like image 31
Jesse Q Avatar answered Jan 25 '23 17:01

Jesse Q