Intel Edison (Poky) bootup services - embedded-linux

Ive been trying to figure out when to set my script to initiate and what to use as After= parameter.
What i need is to initiate my service as late as possible... kind of like the last service in the stack. I def. need /home to be mounted. I cant rely on wpa_supplicant nor mdns since it is not given those have been configured on the device.
Ive also read systemd docs but could not figure out what service to set to After= option in service file.

After=ABC.service
means you service will launch after launching ABC.service, but it is not guarantied, so to ensure you service starts only after ABC.service use
Requires=ABC.service
OR
You can use below script to achieve this.
Create a file at any location of your device once home is getting mounted, and then launch your service
[Service]
Type=oneshot
ExecStart=/bin/ABC -c 'while [ ! -e /tmp/YOUR_FILE ]; do sleep 0.1 ; done'
ABC is your executable of service, it will wait till it doesn't get YOUR_FILE at /tmp/ location.
Hope this helps.

Related

How can I run a Shell when booting up?

I am configuring an app at work which is on a Amazon Web Server.
To get the app running you have to run a shell called "Start.sh"
I want this to be done automatically after booting up the server
I have already tried with the following bash in the User Data section (Which runs on boot)
#!/bin/bash
cd "/home/ec2-user/app_name/"
sh Start.sh
echo "worked" > worked.txt
Thanks for the help
Scripts provided through User Data are only executed the first time the instance is started. (Officially, it is executed once per instance id.) This is done because the normal use-case is to install software, which should only be done once.
If you wish something to run on every boot, you could probably use the cloud-init once-per-boot feature:
Any scripts in the scripts/per-boot directory on the datasource will be run every time the system boots. Scripts will be run in alphabetical order.

How to provide a restart count to systemd service

I have an embedded device which manages its various services using systemd. Our status reporting application is one of these services. It is always on and it automatically restarts on failure (crashes, exceptions, OOM conditions, whatever).
We report an event to our cloud services on device restart (technically application restart) but I'd like to distinguish first start (after reboot) from restart. Is there a mechanism built into systemd which can provide the service restart count, or do I need to roll my own method?
Do you have the journal ? If you do, then you can get the count like this:
journalctl -b -u myservicename.service |grep -c Started
The -b option limits logs to the current boot; -u limits to the service in argument.
Then you grep for the "Started" line, and tell grep to only give you the number of matches.
you can use following command:
systemctl show foo.service -p NRestarts
It will return a value if the service is in a restart loop, otherwise, will return nothing.

Check status of GMediaRenderer

I use GMediaRenderer to send audio via UPNP from a Raspberry Pi. Occasionally, for reasons unknown, I have to SSH into my Pi and send the command sudo service gmediarenderer restart to get it to work properly. I'd like to add a command to crontab or similar that periodically checks whether the service is running properly. I already have a crontab entry that checks whether the service is running, and starts if it isn't. The trouble I'm having is that sometimes, even though the service is running, it doesn't appear to be communicating with UPNP control points. Executing the restart command brings it back, so I assume it is simply the case that the service has crashed but not closed down.
Does anyone know how to programmatically check (preferably using a bash script) whether the GMediaRenderer service is up and running?
I have found a solution to this. The command gssdp-discover returns a list of active renderers. I setup a sudo crontab job to run a bash script every minute that checks whether or not a particular renderer is running, and to restart gmediarenderer if it isn't found.
The following command will list your active renderers:
gssdp-discover -i wlan0 --timeout=3
Change wlan0 above depending on your specific network connection. In my case, the renderer that I'm interested in is listed as urn:av-openhome-org:service:Info:1 (run the command with and without the renderer active, and look for the one that only appears when running). So, my bash script contains the following:
gssdp-discover -i wlan0 --timeout=3 --target=urn:av-openhome-org:service:Info:1 | grep available &> /dev/null
if [ $? == 0 ]; then
echo "OpenHome renderer is already running"
else
echo "restarting gmediarenderer"
/etc/init.d/gmediarenderer stop
/etc/init.d/gmediarenderer start
fi

Gammu: run on receive exit status 1

I'm trying to use gammu and gammu-smsd to send and receive sms with my raspberry pi using a Huawei intrnet key.
My problem is that when I send an sms from my phone to raspberry pi, it read the sms, it try to start the program linked at RunOnReceive = in /etc/gammu-smsdrcn file but then, it says: Process failed with exit status 1.
I tried any kind of solution but I'm not capable to solve this problem by my self; I've set each permission on the script.
Can someone help me?
Thank you a lot.
You no doubt have this sorted by now, but I have just been through the same trip, tore out a lot of hair and finally made it out the back .... :-)
I'm using a ZTE stick with wvdial for internet connection. The stick appears as modems on /dev/USBtty0, 1 and 2. wvdial uses USBtty2, so gammu (I think) has to use a different one.
So I installed gammu/gammu-smsd on USBtty1 in gammu-config and /etc/gammu-smsdrc. The receive daemon gammu-smsd fires up automatically on boot.
First trap for young players - if you want to send an SMS with
echo "whatever" | gammu sendsms TEXT xxxyyyzzzz (where the last is the phone no) - you need to kill the receive daemon for that to work ie
service gammu-smsd stop # kill receive daemon
echo etc etc gammu etc etc # send the SMS
service gammu-smsd start # revive the receive daemon
Now for the RunOnReceive thing ...
start with sudovi - gives some config file to edit. There's a line in there about pi BLAH-BLAH-BLAH as a sudoer. Duplicate it with gammu BLAH-BLAH-BLAH. Same BLAHs. Save it.
It's something to do with permissions - I'm not an expert here :-)
So my RunOnReceive line is { sudo /home/pi/procSMS.sh $SMS_1_TEXT }
The script didn't seem to know what $SMS_1_TEXT was, so I passed it through as a parameter - inside the script it's treated as $1. It works.
While testing I ran a process in another window - just tail -f /var/log/syslog which lets you watch it all in real time ...
I was getting the same error on Raspberry Pi in combination with Huawei E3131 (Process failed with exit status 1) but I solved it.
make sure you have file permissions set well. Gammu runs deamon under "gammu" user by default. So you can change it (/etc/init.d/gammu-smsd) to user who is already located in your system and has rights for executing the script. Or change script permissions by following: chmod 755 script.sh. It means you give execute rights to other users too.
In fact there is additional option. Run gammu deamon with parameter -U username. Unfortunatelly it did not work for me when I used root user.
PS: I would recommend to not to place the script inside /etc directory. Use /home directory instead.
turn on debuging in /etc/gammu-smsdrc. Use parameters: logformat and debuglevel in section smsd. Default log is located in /var/log/syslog. May be it helps you deeply localize the problem.
And the best at the end... I found that gammu returns the error even if it runs the script well! You have to write exit code inside you bash script. If you do not specify an exit code, gammu represents it as error 1. Add exit 0 in case of success in the end of the script and error message disappears.

How to wait in script until device is connected

I have a Sky wireless sensor node and a script which prints the output from the node.
sudo ./serialdump-linux -b115200 /dev/tmotesky1
If I start this script before my pc detects the node, I get the following error:
/dev/tmotesky1: No such file or directory
But if I wait for example 20 seconds, I miss the initial prints (which are important).
Is there a way to detect if the /dev/tmotesky1 exists?
Something like
while [ ! -f /dev/tmotesky1 ] ; do sleep 1; print 'Waiting...'; done
Thanks in advance!
Your code indicates that you are using Linux where you can use the hotplugging mechanism.
On generic systems, you can write an udev rule (--> see with udevadmin monitor -e what happens when you attach the device) which starts e.g. a program or writes something into a pipe. When systemd is used, you can start a service (see man systemd.device).
On small/embedded systems it is possible to write a custom /sbin/hotplug program (set in /proc/sys/kernel/hotplug) instead of using udev.

Resources