1

I am not sure why this is, but I have phantomjs installed on my server

$which phantomjs
/usr/local/bin/phantomjs

and am running a script from crontab as:

0 */5 * * * /home/roy/.rbenv/shims/ruby /home/roy/SCRIPT/lib/SCRIPT.rb

if I just run the exact command from the command line, it will run.

But if it runs from the cronjob, I get:

/home/roy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/cliver-0.3.2/lib/cliver/dependency.rb:143:in 
`raise_not_found!': Could not find an executable ["phantomjs"] on your path. (Cliver::Dependency::NotFound)

I've already seen this in possible duplicate Could not find phantomjs, but even if installed, it still does not appear to be working from cron.

Is there any way to check where this might be going wrong? Just for background this has been working as is for about a month, but broke when I tried to update from phantomjs 2.0.0 to phantomjs 2.1.1.

Community
  • 1
  • 1
roy
  • 3,706
  • 6
  • 30
  • 53
  • It could be that you have a different PATH when you execute the script in the cron job. You could try `puts ENV["PATH"]` in the script and see if it's different. Or you could pass a custom path when invoking the script in the crob job. `PATH=$PATH:/something/custom ruby thescript.rb` – max pleaner Jun 15 '16 at 20:53
  • since you're using ubuntu - you can set the environment variables you want available to cron jobs in /etc/environment -- heres a discussion about it - http://askubuntu.com/questions/700107/why-do-variables-set-in-my-etc-environment-show-up-in-my-cron-environment – Thomas Walpole Jun 16 '16 at 01:43

1 Answers1

1

Problem

When running executables $PATH, which is an array of directories, is searched for that executable.

For example if your $PATH contains /bin you can run phantomjs and /bin will be searched for /bin/phantomjs.

When running scripts from Cron, $PATH contains different directories than what it does when you run that script from bash.

Solution

To run an executable without relying on $PATH simply use its absolute path. An absolute path starts with a /.

  1. Run which phantomjs in bash. This will show phantomjs's absolute path.
  2. In your script replace phantomjs with its absolute path.
Pooyan Khosravi
  • 4,861
  • 1
  • 19
  • 20
  • You're right, I suppose I could do that, but am wondering if there is a way not to have to put it in the script since that is how it has been prior (thought not sure why it just worked then and not now). – roy Jun 15 '16 at 20:48
  • @roy There is. You can call your script like this `bash -l -c '/path/to/script'`. – Pooyan Khosravi Jun 15 '16 at 20:49
  • Or you can set the path in the crontab like someone else suggested – pguardiario Jun 19 '16 at 07:16