Run a script after Bluetooth is configured and running - Rasperry Pi 3 - bluetooth

I'm trying to automatically call a program at boot that uses Bluetooth. However, the program is being called before Bluetooth is configured and running.
I've tried calling the program in two ways:
Using a script in init.d and registered with update-rc.d with this line in the init: # Required-Start: $all
Calling it from /etc/rc.local
Neither of these work as desired. They both start the program, but before Bluetooth is configured and running.
What is the best way to force a script or program to run after Bluetooth?
Below are some select lines from the boot sequence so you can see the issue I'm having:
[ OK ] Started Login Service.
[ OK ] Started Getty on tty1.
**Where my program is currently executing**
[ OK ] Started Configure Bluetooth Modems connected by UART.
[ OK ] Reached Target Bluetooth
**Where I want my program to be executing**
Raspbian GNU/Linux 8 tty1
login:

The new init system for Debian 8 "Jessie" is systemd. The old way in Debian 7 "Wheezy" was Sysv with runlevels and /etc/inittab. A drawback of using crontab to run your program will be, if the script execution crashes, it dies forever. Restarting a script automatically if its ends is called "respawn".
As you can see, the Bluetooth Service is running and prints that a "Target" is reached. To create your own service, which runs after bluetooth startup, and respawns with systemd just create a file in /etc/systemd/system/ i.e. my_program.service
[Unit]
Desription=my_program with systemd, respawn, after bluetooth
After=bluetooth.target
[Service]
ExecStart=node /home/pi/workspace/my_program
Restart=always
[Install]
WantedBy=multi-user.target
and activate it
systemctl enable my_program.service
reboot or start it manually
systemctl daemon-reload
systemctl start my_program.service
If one kills the process or reboots, my_program will be restarted automatically some seconds later.

For anyone running Raspbian 9 (stretch)
I tried out #andpei's answer and my application still wasn't waiting for bluetooth to start up. I was able to solve this by adding "Requires".
[Unit]
Desription=my_program with systemd, respawn, after bluetooth
After=bluetooth.target
Requires=bluetooth.target

Using crontab, I was able to make it work with the following line:
#reboot sleep 5 && node /home/pi/workspace/my_program
Not ideal but it works for now. I'm open to any better answers.

Related

program running a boot on BeagleBoneblack

I am having a problem with a small application I developed on the BBB running Debian Image 2017-03-19.
I connected a barcode scanner to the usb port and a 2x16 LCD display to the GPIO controlled by BBBioLib.
I developed an application in C to read a barcode label apply to a race tyre, which find a match on an SQLite table and show the racer name on the display.
Application work great but since the all assembly must work stand alone I need to run the program automatically at boot.
I follow all the instruction on creating a bash program and service but I am getting a strange behaviour.
The display after the welcome message hang up and never change but the application work correctly because all the printf to the consolle get logged correctly and once I exit the application I can check them on the log of the service.
If I restart the service manually everything work fine.
This is the bash script
#!/bin/bash
/root/read_barcode
This is the service code
[Unit]
Description=Barcode reader launch
After=syslog.target network.target
[Service]
Type=simple
ExecStart=/usr/bin/barcode.sh
[Install]
WantedBy=multi-user.target
Does anyone can help on solving this problem.
Thanks
Carlo
Run a .service file with sudo systemctl enable YourService.service at this location.
/etc/systemd/system/
Use the enable option for systemd .service files to make your source work on boot or a reboot.

Enabling systemd service with preseed

I'm trying to enable serial-getty#ttyS0.service to output getty to serial console as well as tty0.
I have tried running systemctl enable serial-getty#ttyS0 but I suspect that systemd might not be running when late_command is running.
Then I tried making the symlink manually with ln -s /lib/systemd/system/serial-getty#.service /etc/systemd/system/getty.target.wants/serial-getty#ttyS0.service and after the install getty.target.wants only contains getty#tty1.service.
I can't use console=tty0 console=ttyS0,115200n8 kernel parameter because then messages from the init system and the system logger will only appear on the first serial port and I want them to appear on tty0.
Does anyone have any experience or any idea on how to enable services in preseed config?
Thanks.

How do I start a service on boot in debian?

So I created a service and made it executale in /etc/init.d
Then I tried to make it start after boot with the following command:
update-rc.d <myService> defaults
But the output were warnings of other services (which work just fine by the way) and I don't want to touch those.
My next attempt to solve this was to add the following lines in the rc.local file:
do_start() {
service <someOtherService> stop
service <myService> stop
sleep 5
service <someOtherService> start
service <myService> stop
........
As I said, I have some other service that works just fine, but my service which I am currently trying to add wont start after boot.
One more thing that I would like to add is when I manually enter:
service <myService> start/stop
then it works just fine
To add a service at boot, you can add an Upstart job.
Create your conf file in /etc/init (e.g. /etc/init/myjob.conf)
with a content like this (Example)
description "My job"
start on startup
task
exec /path/to/script.sh
More informations here (Debian doc)
Info: you need the upstart package.
The following works very well for me.
First determine if your system is running SysV init or systemd, for that use:
$ ps -p 1
If SysV init:
$ sudo update-rc.d <service_name> defaults 95 10
If systemd:
$ sudo /bin/systemctl daemon-reload
$ sudo /bin/systemctl enable <service_name>.service
I successfully used the last one on a ParrotSecurity OS that is based on Debian 5.

Trigger CURL Request after boot using systemd

I'm trying to create a service that will trigger every time a raspberry pi boots. Currently the service runs a really simple script that sends a POST request to a web service endpoint I control. I can trigger said script manually and that part all works perfectly.
I'm struggling with the next step which is to get that script to run after the pi has finished booting. I also need to be able to get it to run without a user logging in.
CURL Script (algiers-startup.local)
#! /bin/bash
echo "Attempting CURL Request"
curl --data "param1=value1&param2=value2" http://10.68.159.28:3000/device
Systemd Service
[Unit]
Description=Algiers RaspberryPi Startup
After=network.target
Before=getty#tty1.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/algiers-startup.local
TimeoutSec=30
StandardOutput=tty
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
I see no errors or outputs in the console, no hint that anything has happened at all.
I’ll assume your machine is already set up with Systemd. It’s controlled primarily through the systemctl command. I alias it as such since it’s awful to type all the time:
alias sc=systemctl
alias ssc='sudo systemctl'
You just need to “enable” your service to have it run at boot:
sc enable algiers-startup
I’m not sure what distro you’re using, but on Arch and CentOS, you’ll want algiers-startup to live down in /usr/lib/systemd/system/.
You can test your service with sc start algiers-start. journalctl can show you what’s happening.

Linux: Start daemon on connected USB-serial dongle

On my Linux (Angstrom distro on BeagleBone Black) I have a USB dongle which presents as a serial port and per default is available as /dev/ttyUSB0
I want to start a daemon, which will connect to the serial port and make it available as a socket. I have the code for this USB-to-socket bridge and it works when started by hand.
I want it to start automatically whenever the system boots, supposing the USB dongle is plugged in. How should I do this?
Attempts so far:
systemd: I created a systemd service with conditions After: remote-fs.target and After:syslog.target , but (it seems) the USB dongle is not ready at that point and the startup of the daemon fails.
Are there other systemd targets or services to condition to, so that the daemon is started only when the udev has finished installing devices and the network is ready?
udev: I created a udev rule like
KERNEL=="ttyUSB?", RUN+="/path/to/daemon.sh"
which executes successfully. But the daemon (which is started as a background process with a "&" within that script) seems not to execute. Also it seems to be frowned upon, to fork long running processes from udev rules.
What is the correct way to do it?
Create a udev rule like
# cat /etc/udev/rules.d/95-serialdaemon.rules
KERNEL=="ttyUSB0", TAG+="systemd", ENV{SYSTEMD_WANTS}="serialdaemon.service"
Create a systemd service like
# cat /lib/systemd/system/serialdaemon.service
[Unit]
Description=USB serial to socket bridge
After=remote-fs.target
After=syslog.target
[Service]
ExecStart=/mnt/serialdaemon.sh
Create the executable file
# cat /mnt/serialdaemon.sh
#!/bin/sh
date +%F-%T >> /var/log/serialdaemon.log
/usr/local/serialdaemon/serialdaemon -serial /dev/ttyUSB0 -port 15789 -baud 38400 >> /var/log/serialdaemon.log 2>&1
date +%F-%T >> /var/log/serialdaemon.log
Since the link in my further comment seems to solve this problem, here is the solution for using udev for starting a daemon when a certain usb device is plugged in:
Proper(-ish) way to start long-running systemd service on udev event (device hotplug)

Resources