I needed to create a bash shell script tonight that called another shell script with options that then would load a Ruby environment and execute certain commands within a Ruby project. I ran into numerous issues but most were silly things such as typos or other minor issues caused by myself. The one issue I had a little trouble figuring out because of the file that was causing the error related to cron not being able to run “/usr/bin/env: ruby”. My bash script worked perfectly from the Linux CLI shell however when attempting to run it I would always get the same error. Below I list details about the error, where I finally located what the error was, and how to resolve the error.
How The Bash Script Run From Cron Error Was Discovered:
I was outputting the results of cron to a log file at first but I was unable to see any errors. The script would be called, log a couple basic commands I put in to make sure it was running, and then stop without a single error. There were also no errors showing up in the cron log file located at /var/log/cron. Finally I removed the “>> somefile.log” at the end of the cronjob to see if an error would display in the email for the user running the cronjob. Sure enough when reviewing the mail for that user the error below was displayed in the contents of each email sent from cron.
Bash Script Run From Cron Error Locating Ruby:
It turns out more output was being sent to the email for the user running cron versus how I had the output going to a log file. To check mail from the command line in Linux just su to that user and type mail as displayed below.
Check User Email From Linux CLI:
[root@sdev ~]$ su - web [web@sdev ~]$ mail Mail version 8.1 6/6/93. Type ? for help. "/var/spool/mail/web": 2 messages 2 unread >U 1 email@example.com Thu Dec 2 00:42 76/5850 "Cron /home/web/ruf-restart.sh >> /home/web/ruf-restart.log"
The above shows would display if you only had one email waiting for that user. To read that email from within the mail command line application just click enter. If there are numerous emails waiting you can continue clicking enter to read through the email. Anyhow when I read the email that was waiting the below errors displayed.
Cron Bash Script Errors In Cron Users Email Inbox:
Thu Dec 2 00:38:01 CST 2009 Starting Rufus Job Restart Cron /usr/bin/env: ruby: No such file or directory /usr/bin/env: ruby: No such file or directory /usr/bin/env: ruby: No such file or directory Ending Rufus Job Restart Cron Thu Dec 2 00:38:11 CST 2009
The contents above display output from the “date” command, a echo message explaining the process, three attempts to restart rufus which makes ruby calls, a echo message explaining the process has ended, and the specific time the script ended also displayed using date. So now I had to figure out why the error below was displaying.
Error:/usr/bin/env: ruby: No such file or directory
The path lead from my shell script calling rufus.sh to rufus.sh calling config/jobs/control.rb to control.rb calling script/runner. At the top of script runner was “#!/usr/bin/env ruby” which I needed to change to “#!/usr/local/bin/ruby” to resolve the issue. Cron was unable to run “env ruby”. So again to resolve this error simply modify the runner file to look like the below.
Modify The Top Line Of “project-root/script/runner”:
#!/usr/local/bin/ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/runner'
The bash script now runs from cron without any issue.