obabarin
Posts: 12
Joined: Tue Oct 09, 2018 10:25 am

Bash script as Cron job to run at reboot

Mon Oct 15, 2018 11:32 am

Dear,

I created the following bash script for data logging from uart port using Grabserial which works find from the terminal.

Code: Select all

#!/bin/sh
sleep 2
grabserial -v -d /dev/ttyAMA0 -T -b 115200 -e 3600 -Q -o "/media/pi/E27F-7413/PiData/file-%d-%m-%Y-%H.%M.%S.txt" -a
I will want this script to run immediately after my Rpi3 has boot up, this I tried to achieve by adding cron jobs. But I could not getting it working. The various cron jobs I have tried are follows and none worked.

Code: Select all

# @reboot sudo /home/pi/Documents/Grabber/./grab.sh
# @reboot sudo /home/pi/Documents/Grabber/./grab
# @reboot sudo /home/pi/Documents/Grabber/grab.sh
# @reboot sudo /home/pi/Documents/Grabber/grab

# @reboot /bin/sh sudo /home/pi/Documents/Grabber/grab.sh
# @reboot /bin/sh /home/pi/Documents/Grabber/grab.sh
# @reboot /usr/bin/sh sudo /home/pi/Documents/Grabber/grab.sh
@reboot /usr/bin/sh /home/pi/Documents/Grabber/grab.sh
*I do remove the # before running/rebooting the RPi*

I have been able to successfully run cron jobs of C/C++ programs before now. I simply followed the examples of my previous exercises. The following programs had been tested to work successfully.

Code: Select all

# @reboot sudo /home/pi/Documents/myC/./uartStart
# @reboot sudo /home/pi/Documents/myC/./tictocuart
Any useful suggestions please?

NB- in the terminal window, I can successfully run the script using

Code: Select all

Documents/Grabber/./grab.sh

DirkS
Posts: 9259
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Bash script as Cron job to run at reboot

Mon Oct 15, 2018 11:39 am

I see your script uses a mounted drive.
Is the drive mounted at the time the script is executed? If not it will probably fail.

For a quick test I would set the output to a local directory (e.g. /home/pi)

obabarin
Posts: 12
Joined: Tue Oct 09, 2018 10:25 am

Re: Bash script as Cron job to run at reboot

Mon Oct 15, 2018 3:03 pm

DirkS wrote:
Mon Oct 15, 2018 11:39 am
I see your script uses a mounted drive.
Is the drive mounted at the time the script is executed? If not it will probably fail.

For a quick test I would set the output to a local directory (e.g. /home/pi)
Thanks for the tip. I changed the directory to save files to local memory other than external, and then tried all the previous commands. None seems to work for me. Any other suggestions please?

User avatar
RaTTuS
Posts: 10110
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Bash script as Cron job to run at reboot

Mon Oct 15, 2018 3:06 pm

don't use cron
use a systemd .service file
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

obabarin
Posts: 12
Joined: Tue Oct 09, 2018 10:25 am

Re: Bash script as Cron job to run at reboot

Mon Oct 15, 2018 6:11 pm

RaTTuS wrote:
Mon Oct 15, 2018 3:06 pm
don't use cron
use a systemd .service file
Thanks for the suggestion. I tried out using the systemd, but it keeps restarting the script. I can't seem to find an option for not restarting. None of the parameters- no, always, no-success, on-failure, on-normal, on-abort seems to work appropiately.

Any other suggestions please?

DirkS
Posts: 9259
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Bash script as Cron job to run at reboot

Mon Oct 15, 2018 7:18 pm

obabarin wrote:
Mon Oct 15, 2018 6:11 pm
Any other suggestions please?
Instead of just saying 'it doesn't work' give us something to go on, such as (error) messages.
Redirect stdout an stderr to a file. Something like

Code: Select all

command >/home/pi/output.txt 2>&1
Also post your systemd service file.

obabarin
Posts: 12
Joined: Tue Oct 09, 2018 10:25 am

Re: Bash script as Cron job to run at reboot

Tue Oct 16, 2018 9:59 am

DirkS wrote:
Mon Oct 15, 2018 7:18 pm

Redirect stdout an stderr to a file. Something like

Code: Select all

command >/home/pi/output.txt 2>&1
Also post your systemd service file.
All right @DirkS.

Here is the journalctl out of the pigrab.service:
-- Logs begin at Thu 2016-11-03 18:16:42 CET, end at Tue 2018-10-16 11:50:44 CEST. --
Oct 16 11:46:02 raspberrypi systemd[1]: Started serial data logger with grabserial.
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Opening serial port /dev/ttyAMA0
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: 115200:8N1:xonxoff=0:rtscts=0
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Program set to restart after 3600 seconds.
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Printing absolute timing information for each line
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Saving data to '/home/pi/file-16-10-2018-11.46.09.txt'
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Keeping quiet on stdout
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Use Control-C to stop...
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Traceback (most recent call last):
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/bin/grabserial", line 4, in <module>
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: __import__('pkg_resources').run_script('grabserial==1.9.8', 'grabserial')
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 739, in run_script
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: self.require(requires)[0].run_script(script_name, ns)
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1501, in run_script
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: exec(script_code, namespace, namespace)
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 588, in <module>
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]:
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 500, in grab
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]:
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: TypeError: encode() argument 1 must be string, not None
Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Main process exited, code=exited, status=1/FAILURE
Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Unit entered failed state.
Oct 16 11:46:09 raspberrypi systemd[1]: pigrab.service: Failed with result 'exit-code'.

And also is my bash script

Code: Select all

#! /bin/sh
sleep 5
grabserial -v -d /dev/ttyAMA0 -T -b 115200 -e 3600 -Q -o "/home/pi/file-%d-%m-%Y-%H.%M.%S.txt" -a

DirkS
Posts: 9259
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Bash script as Cron job to run at reboot

Tue Oct 16, 2018 11:29 am

obabarin wrote:
Tue Oct 16, 2018 9:59 am

Code: Select all

Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: Traceback (most recent call last):
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/bin/grabserial", line 4, in <module>
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: __import__('pkg_resources').run_script('grabserial==1.9.8', 'grabserial')
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 739, in run_script
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: self.require(requires)[0].run_script(script_name, ns)
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1501, in run_script
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: exec(script_code, namespace, namespace)
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 588, in <module>
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: 
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: File "/usr/local/lib/python2.7/dist-packages/grabserial-1.9.8-py2.7.egg/EGG-INFO/scripts/grabserial", line 500, in grab
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: 
Oct 16 11:46:09 raspberrypi pigrab.sh[2728]: TypeError: encode() argument 1 must be string, not None
There's your problem. grabserial crashes on line 500 (https://github.com/tbird20d/grabserial/ ... erial#L500)
Seems to be a problem with 'sys'stdout.encoding'.
Looks like the behaviour is different between running in the foreground or from cron / system service file.
ATM I have no idea how to fix it; you may want to contact the developer.

obabarin
Posts: 12
Joined: Tue Oct 09, 2018 10:25 am

Re: Bash script as Cron job to run at reboot

Tue Oct 16, 2018 8:39 pm

Thanks for the comments. The developer is yet to respond.
But I found a work around here https://www.raspberrypi.org/forums/view ... 6#p485866 . Though this comes with some inconvenience.

Return to “Troubleshooting”