Cron (hourly) task to execute a ruby script - ruby

I have a ruby script that I have tested to be working that I would like to run as an hourly cron but cannot seem to get it firing properly.
The last thing I have tried was placing the line:
ruby ~/ruby_script.rb
in /etc/cron.hourly
Said ruby script is located in the home directory with:
#!/usr/bin/env ruby
as its top line.
I have looked into ruby & cron resources but they most seem to be for reoccurring tasks in a Ruby on Rails environment when I just want the script to run in my ubuntu environment. I have double checked that rails is installed as well.
I have had a lot of fun learning more about ubuntu over the past few months and will truly appreciate any assistance I receive here. Thank you in advance.

Try to use current user crontab
$ crontab -e
Add new cron job
0 * * * * /bin/bash -l -c 'ruby ~/ruby_script.rb'
Using bash command to run helps to get env variables during script execution.
(for tests change 0 to * - script will try to run every minute)
To log errors you can try to add command:
0 * * * * /bin/bash -l -c 'ruby ~/ruby_script.rb >> ~/ruby_script.log 2>&1 &'
Hope it helps.

Related

Crontab, can't execute all the script

I need to run daily a shell script which contains the steps of the model Weather Research Forecasting. I used cron for this. The first executable of the script (ungrib.exe) works perfect, but the second executable "metgrid.exe" it's not working at all. When I run the script in the terminal of linux works perfectly.
This is an example (summarized) of my script:
#!/bin/bash
bash
./link_grib.csh /home/user/WRF/GFS/
./ungrib.exe
ln -s metgrid/METGRID.TBL.ARW ./METGRID.TBL
./metgrid.exe <- not running with crontab
The way that I configure my crontab is:
crontab -e
SHELL=/bin/sh
00 01 * * * /home/user/WRF/scripts/WRF_scripts.sh
Any idea?
I added in my crontab the full path of libraries related with the execution of WRF.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH

Simple bash script doesn't cron properly

I'm attempting to cron a simple bash script on my macbook-pro laptop. Ultimately, I would like to first get this to work for bash script and then move on to my python scripts. I've created a simple bash file (named hello.sh) with the code below:
#!/bin/bash
echo "Hello World" >> /Users/myusername/Desktop/test.txt
And my crontab -e is designated as follows:
* * * * * /bin/bash /Users/myusername/Desktop/bash-files/hello.sh
However, I get nothing after waiting a minute.
After googling around, I concluded that maybe I was running into the "gotcha" issue (cron reading different parameters than env). So I queued the following:
* * * * * env > /tmp/env.output
and it's output as follows
SHELL=/bin/sh
USER=myusername
PATH=/usr/bin:/bin
PWD=/Users/myusername
SHLVL=1
HOME=/Users/myusername
LOGNAME=myusername
_=/usr/bin/env
running env in my terminal produces the following relevant parameters:
SHELL=/bin/zsh
USER=myusername
PATH=/Users/myusername/opt/anaconda3/bin:/Users/myusername/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
PWD=/tmp
SHLVL=1
HOME=/Users/myusername
LOGNAME=myusername
_=/usr/bin/env
I've added the above parameter settings to my hello.sh script but I still get nothing.
Can anyone point out to what my issue is here?
These are permission and path errors, easily resolved.
Look in system preferences to grant full disk access to your binaries and unset PATH in your scripts to catch any paths that are not complete.
I recommend /usr/local/bin for ease of maintaining any scripts you wish to have launchd or cron to schedule.
There’s no reason you can’t run from your user folder if that suits you, however.

Shell script doesn't run properly while running from crontab

I read the other related topics but they didn't help me.
I have a shell script which checks if my python script is not running,it will run it. Otherwise it will just skip and do nothing.
It totally works when I use:
bash myshellscrip.sh
And I get the result that I want which is doing some tasks and sending emails to some correspondents. However, when I try to run this particular shell script on crontab, it doesn't send out the emails and doesn't do the other tasks.
I tried the following on crontab and none of them worked.
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh >> /some/other/path/output.txt
When I save the changes into 'output.txt' file, it creates the file but it doesn't send the emails or doing other tasks.
I also tried the option of reboot because I need this program to run at start up too, and this didn't work:
#reboot /bin/bash /path/to/my/script/myshellscrip.sh
Does anyone know how to fix it?
EDIT:
As I was checking with the simplest shell scrip like:
#!/bin/sh
/usr/bin/python /home/pi/DCA/code.py
My crontab wouldn't have any output in my output.txt file although my code.py have something printing out, too.
However, when I use a very simple python code for example only a 'print' statement it will run and save the output into output.txt.
Seems like your shell script crashes / stops before it can do something (possibly due to the environment being different or permission issues). You can check /var/log/syslog to find out.
You could try removing /bin/bash, I don't think that's necessary?
Run the cron job in debug mode. for that, Add -x to the bash command on the cronjob and save their output in the file.
bash -x /path/to/script.sh >> /path/to/the/output.txt
You can find the problem.
Apparently crontab was running my script several times. So I tried to use different locking mechanisms to put a lock around my scrip but only using flock worked for me. In my crontab I added this line:
* * * * * /usr/bin/flock -n /tmp/ms.lockfile /bin/bash /path/to/my/script/myShellScript.sh

Process stops after start in cron

I want to start webrtc2sip from cron:
* * * * * root /taafs/webrtc2sip/webrtc2sip --config=/taafs/webrtc2sip/config.xml >> /var/log/webrtc2sip.log 2>&1
When cron runs, I see output in /var/log/webrtc2sip.log , but
ps aux | grep webrtc2sip
shows nothing.
When I run command from my root bash it shows absolutely the same output, and shows in ps aux!
Also, when I install env from cron to new bash, and start command from this bash, it works(displayed in ps)! So the reason is not environment, but what?
I had a similar issue, so I resorted to creating a small patch to make console optional when starting webrtc2sip by adding a new argument, --without-console.
It is documented here: http://marcelog.github.io/articles/starting_webrtc2sip_as_service.html and the patch is available as a gist here: https://gist.github.com/marcelog/47ef364b43c67f9f359d
I hope it helps, it's been working great for me so far, and I use daemontools to supervise the process.
Best!

Crontab with Ruby script returning error code 1

I'm rewriting some shell scripts in Ruby that will be used to backup specific content from our site. I am running Ruby 1.8.7 and have the script running locally as root and it works fine.
I have just attempted to add the script to cronttab on Ubuntu 11.10.
*/5 * * * * ruby /root/code/backup_images.rb local
When the cron runs I receive the following error in the syslog:
Mar 21 16:15:01 ubuntu CRON[4942]: (root) CMD (ruby /root/code/backup_images.rb local)
Mar 21 16:15:02 ubuntu CRON[4941]: (CRON) error (grandchild #4942 failed with exit status 1)
I've tried the following solutions but to no avail:
env -i $SHELL --norc
Which supposedly creates a new naked shell that mimics what crontab runs and this works fine with my script.
#!/usr/bin/env ruby
I have added that declaration to the top of my script which hasn't changed the functionality when running it manually nor has it resolved the issue with the crontab.
I have even tried to output the results of the call to a file, however that only creates a new file with nothing in it.
I'm not sure what else could be the problem, but I'm assuming it has to be something with Cron not being able to find Ruby or something of that nature. I'm rather new to this, so I'm reaching a dead end here.
Thanks in advance!
I believe the fault is that you need to apply the path to ruby in your crontab
but personally I prefer just to path the ruby file to be executed.
*/5 * * * * /root/code/backup_images.rb local
and then append
#!/usr/bin/env ruby
to the top of backup_images.rb
you also need to make the ruby file executable with
chmod +x
So with the help from #tomodachi I went a step further and explicitly wrote out the following crontab line and it works now.
*/15 * * * * cd /root/code/ && ./backup_images.rb local > /root/code/log/script.log
So basically I tried to execute the command from a bash shell and it wasn't finding the script. So I explicitly go into the direct and then execute the backup script.
Everything worked fine from this point forward.
Thanks of your help.

Resources