Running casper.js script from cron

I am trying to run casper.js script through cron. Everything works fine when I run the script manually, but when I run it through 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:

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

I also tried

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

This gives me the same result. Any ideas? I suppose this might be a path problem, but I don't know where to go from here!

+4
source share
3 answers

You should probably use the 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:

Well, that was a little stupid. You can also set your own path to the phantomjs by setting the environment variable PHANTOMJS_EXECUTABLE :

 $ 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 

Tip. If your crontab is running as a different user, make sure that he has access to the phantomjs path.

Hope this helps (and works).

Change again

Wait, the stack trace says that you are using an old version of CasperJS (for example, the subprocess module is no longer used). Try a newer version :)

+4
source

This is an old question, but still relevant - I just spent 4 hours trying to solve it without finding a direct solution. What happened to me was exactly the same, I could run the casper.js script from the shell command line, but not through the cron job. As stated in NiKo, but not clear enough to me , in order to gain access, you need to know where Phantomjs works.

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 
+3
source

This is an older, older question, but I've been doing this for a while too. I could no longer restart Apache (for Django) via cron (talk about downtime), and this eventually became a problem. Instead of using a wrapper script like Jesse Q, I just added my casperjs / phantom -augmented PATH to the TOP of my crontab

 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 

and now it works. You can test your cron environment by writing a file in crontab as follows:

 * * * * * env > ~/cronenv 

just vim into it to find out what your current cron PATH is. Nice to debug this problem.

+2
source

Source: https://habr.com/ru/post/1413781/


All Articles