Process stops after start in cron - bash

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!

Related

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

Cron (hourly) task to execute a ruby script

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.

Cron job does not run

Following is the entry in the crontab:
MAILTO=abc#gmail.com
45 14 * * * /home/user/simple.sh
I've also done chmod +x on the simple.sh But the crontab does not run, it doesn't even send an email.
pgrep cron shows an Id. I also tried bouncing crond. But no luck!
Could someone please point out the mistake here
The simple.sh script is:
#! /bin/bash
echo hello
Thanks
Since you are doing a echo within the cron job script, you need to capture its output somewhere.
Your shebang and file mode (using chmod +x) are all right, so those aren't the issue here and running without /bin/sh should work fine.
Try using the following to see the output in cron.log file (This runs every minute)
* * * * * /home/user/simple.sh >> /home/user/cron.log
Note that cron jobs run in separate subprocess shell, with reduced environment, so its output won't be visible on your terminal.
Regarding sending of email - you need to have some mail package (like postman, mutt etc) configured for the cron daemon to send out error mails.
Do not use relative paths, but absolute ones. Also, indicate the binary running the script, that is /bin/sh (or whatever coming from which sh):
45 14 * * * /bin/sh /path/to/script/simple.sh
Maybe there shouldn't be a space in line 1 of your .sh script:
#! /bin/bash
to
#!/bin/bash
Although I could see why it would still seem to work from when invoked in an interactive shell (# could merely comment out the rest of the line).
Still, I'd guess at worst it'd merely ignore that line and inherit cron's interpreter of /bin/sh

Running debian bash file as cron job

I can't seem to run a simple bash file as a cron job that runs once a minute.
#!/bin/bash
NET_INTERFACE=eth0
CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`
wget -q --delete-after "http://abc.co.uk/raspiUpdate.php?pi=1&ip=${CURRENT_IP}"
In the crontab I have:
* * * * * /home/user/Scripts/script.sh
Where am I going wrong?
Thanks
To troubleshoot this cron issue a few steps come to mind:
double-check the scripts file name you put in your crontab:
$ ls -lah /home/user/Scripts/script.sh
$ cat /home/user/Scripts/script.sh
check that that script is executable by the user the crontab is installed for. i.e. if you use /etc/crontab, it should be executable by root, if you used
$ crontab -e
it should be executable by your userid.
Actually running that script should already give you a hint.
look for hints provided by cron. Depending on what distribution you run, the file cron logs to is different. For debian it should be /var/log/syslog IIRC
If you can't find an error message anywhere, it's hard to tell what's wrong. Generally if I reach that point, I stop and go to sleep or do something else.
[edit]
And when I return, i often find the error. :)
p.s.:
i recently had problems with dots in the filename. Some cron implementations ignore files in /etc/cron.* with dots in their filename. Documented here: http://manpages.ubuntu.com/manpages/xenial/en/man8/cron.8.html as Debian specific. But I think I had this issue also on Fedora.

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