简体   繁体   中英

Unable to execute Ruby script in a Python script

I'm having trouble executing a Ruby script with my Python code.

My server has a cron job that is supposed to execute a Python script and a Ruby script. However, the Ruby script has to be executed after the Python one, so I decided to add a line:

os.system("ruby /home/username/helloworld.rb") 

at the end of the Python script.

It runs, but I'm getting this error in the log file:

/bin/sh 1: ruby not found

I'm not sure why this is happening; I've tried calling the exact same function in the Python console as well as running the Python script manually, and both work perfectly. In other words, this line of code doesn't work ONLY when the script is triggered by cron.

Is there something else I need to put in my crontab/Python script perhaps?

Cron passes only a very limited number of environment variables to your job. According to the CRONTAB(5) Man Page :

  • SHELL is set to /bin/sh
  • PATH is set to /usr/bin:/bin
  • LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.

HOME , PATH and SHELL may be overridden by settings in the crontab; LOGNAME may not.

So if your ruby executable is not located in either /usr/bin or /bin cron cannot find it by default.

You can a specify PATH within crontab to include your ruby executable though.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

17 * * * * python my_ruby_calling_script.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM