run program when usb connected to raspberry pi - linux

I'm trying to run a program when I plug USB to my Raspberry Pi 3 model B.
I referenced below two
https://hackaday.com/2009/09/18/how-to-write-udev-rules/
https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
write a *.rules file in /etc/udev/rules.d/ directory
sudo nano /etc/udev/rules.d/81-usb.rules
fill with
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
restart udev
sudo /etc/init.d/udev restart
I almost tried every variation, rebooted a lot, just in case I'm missing something. But nothing works.
Thanks for reading.

Thanks stark.
Anyway I'm answering my own Question.
I changed it from
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
to
KERNEL=="sda1", RUN+="/home/pi/test.sh"
also did
chmod +x /home/pi/test.sh

Related

Playing a video on Raspberry Pi OS startup

I need to run a script to play a video when I turn on my Raspberry Pi 4. I'm using crontab to run my script which opens a video with mpv.
When I run the script normally, it works fine and the video is being played.
The problem is, when i boot the raspberry, the script automatically runs but mpv doesn't .
here is my script:
#!/bin/bash
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"
while true; do
echo "playing "+$VIDEOPATH
$SERVICE --fs --start=00:00:00 $VIDEOPATH
done
I added this line to crontab:
#reboot /home/pi/Desktop/my-script.sh
I'm totally stuck in this. Any help saves my life!
The most practical solution I found to run GUI programs on startup is using Autostart.
I created a .desktop file at /etc/xdg/autostart directory:
sudo nano /etc/xdg/autostart/display.desktop
when display would be a custom name for my script.
I added following lines the display.desktop :
[Desktop Entry]
Name=Play a video
Exec=mpv --fs --start=00:00:00 path-to-my-video
Saved the file and reboot the Pi.
sudo reboot
As soon as my Pi boots up, my GUI program automatically start as well.
Please update your script:
#!/bin/bash
source ~/.bash_profile
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"
while true; do
echo "playing "+$VIDEOPATH
$SERVICE --fs --start=00:00:00 $VIDEOPATH
done

/dev/ttyACM0 access denied on Lubuntu boot, but works any other time

I am trying to launch a program on Lubuntu 19.04 that talks to my Arduino Due device (robotics) via serial with /dev/ttyACM0. Everything seems fine, until I try to launch the app at startup.
The problem occurs only when I try to make my app boot at login. Running it as user, from terminal, from .desktop file, from script etc. is not a problem at all, I am able to establish connection 10/10 times.
Please bear in mind that I am putting $USER manually now, in every command it is explicitly passed as my de facto username.
It shouldn't be a permissions problem.
$ ls -la /dev/
...
crw-rw-rw- 1 root dialout 166, 0 mar 13 17:27 ttyACM0
...
$ groups $USER
$USER : $USER root adm tty dialout cdrom sudo dip plugdev lpadmin sambashare
I have tried runing it from .xsessionrc with:
#!/bin/bash
# Some other commands to set the environment up
/home/$USER/user_panel /dev/ttyACM0
I have also tried setting it in LXQt Session Settings with Autostart so i ended up with a .desktop entry:
$ cat ./.config/autostart/$USER\ User\ Panel.desktop
[Desktop Entry]
Exec=/home/$USER/user_panel /dev/ttyACM0
Name=User Panel
Type=Application
Version=1.0
I have tried launching both of that files manually and it was alright, the app started, the connection was made.
I have also tried making my udev rule.
$ cat /etc/udev/rules.d/50-myusb.rules
KERNEL=="ttyACM[0-9]*", MODE="0666
Hence, my question is: what should I look for as the reason for this boot time denial?
I don't know if this is the same problem but I was getting "access denied" when opening /dev/ttyACM0 during a hot-boot of the USB connection. I went through permissions, overrode the kernel to give me ownership of the device and still got the same error. Unless I was debugging, in which case access was fine.
The problem seemed to be that the device had not yet started up and putting a half second delay (you may have to increase it to find what works) was enough to allow the device to initialize and be accessed correctly. Try putting a short delay inside a finite loop while you try to open it.
Hope this helps.

Linux - shutdown-script with SSH

I would like to make a shutdown-script for my raspberry pi to shut down anothe raspberry pi over ssh.
The script works if it is running itself but at the shutdown routine the ssh command is not executed.
So that I have done until now:
Made the script in /etc/init.d:
#!/bin/sh
# the first thing is to test if the shutdown script is working
echo "bla bla bla " | sudo tee -a /test.txt
ssh pi#10.0.0.98 sudo shutdown -h now
Made it executable
sudo chmod +x /etc/init.d/raspi.sh
Made a symlink to the rc0.d
sudo ln -s /etc/init.d/raspi.sh /etc/rc0.d/S01raspi.sh
Now I know so far that the shutdown script is working outside of the shutdown routing by calling itself and the shutdown symlink I made is also working partially because I see the changes in the test.txt file every time I shut down.
Can anyone help me how to solve my problem?
Have you tried with single quotes?
The first link in Google has it
http://malcontentcomics.com/systemsboy/2006/07/send-remote-commands-via-ssh.html
What about the sudo, how do you solve entering the password?
https://superuser.com/questions/117870/ssh-execute-sudo-command
Please check this or other links on the web that have useful information.
I would have send all this in a comment but I cant yet because of reputation.
I have now got the script running by myself. I do not really know why it is now working but I write it down beneath and maybe someone else can clearifiy it.
I don´t think the first two changes at my system makes a difference but I also write it down. In the meanwhile because I do not managed the script to get working I had made a button to shutdown the system manually. Also I made a script which backs the mysql-database up (which is on the Raspberry Pi which I would like to switch off with the script) and copies the backup to the raspberry pi which should switch of the other raspberry automatically via the shutdown-script. This happens with scp and also for the password is a key generated.
I have also changed my script to get a log-message out of the script.
#!/bin/sh
ssh -t -t pi#10.0.0.99 'sudo shutdown -h now' >> /home/osmc/shutdown.log 2>&1
To get it into the shutdown-routine I used:
sudo update-rc.d raspi-b stop 01 0
I hope somebody can say me why my code now worked on the first day but not on the next few days until now.
I structured a command to suspend or shutdown a remote host over ssh. You may find this useful. This may be used to suspend / shutdown a remote computer without an interactive session and yet not keep a terminal busy. You will need to give permissions to the remote user to shutdown / suspend using sudo without a password. Additionally, the local and remote machines should be set up to SSH without an interactive login. The script is more useful for suspending the machine as a suspended machine will not disconnect the terminal.
local_user#hostname:~$ ssh remote_user#remote_host "screen -d -m sudo pm-suspend"
source: कार्यशाला (Kāryaśālā)

How to initialize Bluetooth in a startup script with Yocto Poky Linux

I have a script that initializes my bluetooth setup on an Intel Edison. It allows pairing and connecting to this headless machine running Yocto Poky Linux. It was suggested that I put the startup script in /etc/init.d and run update-rc.d myscript.sh defaults. The script ran but it didn't work (generated boot errors saying bluetooth device not found) because Bluetooth had not started yet. I did some reasearch and after removing my links I did update-rc.d myscript.sh defaults 99 which was claimed to run the script last but it did't make any differrence -- it still ran in the same place in the boot sequence. I verified that the links had S99 on them so it seemed like they were set up correctly. There is another post on SO asking a similar question but that was a Ubuntu system where mine is Poky Linux. That solution suggested putting the startup script in a directory that does not exist on my system. There were other suggestions, putting it in rc.local, which I did and got the same result, it runs before Bluetooth is initialized.
Here is my script. My program is called nmea_thread and is run last. Everything else is initializing Bluetooth.
#!/bin/sh
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
Often bluetooth is initialized asynchronously, so you can't be sure that your script will be run after hci0 is added. Good solution is to wait for BT initialization in background:
#!/bin/bash
if [ "$1" != "background" ]; then
$0 background &
else
#Wait until BT is initialized
for ((i = 0; i <= 100; i++)) do
hciconfig hci0 && break
usleep 100000
done
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
fi
hciattach is the correct way.
syntax
hciattach /dev/ttyAMA0 bcm43xx 3000000
you need to flash the driver first before initializing it. Currently i don't remember how, but thats how i made it with raspberry pi and yocto.
Note if you use systemV, you can do call it from a script and it will work
Using SystemD, you need to make it in a service and wait. Falshing should be done in the two cases.

Run a c-programm on startup and see echo via ssh on Raspberry Pi

I have found many ways of starting a script or program when booting Linux, but none of the options is sufficient for what I am looking for:
I have a Raspberry Pi running raspbian wheezy
I have a compiled c-program which creates echo-outputs from time to time
I would like the program to run on startup of the pi and be able to connect via ssh and see what is going on in the program (get echo-live-output, not only a log-file).
Is there any way of achieving this?
To run the program at startup, put it in the root crontab and use the #reboot option. Why don't you want to redirect the output to a log file? Then you can monitor it's output in many ways (eg. using scp).
For anyone encountering a similar problem: I solved it the following way:
I created a startup-bash-script including
sudo /foo/main.o | tee /foo/log;
which writes the output into a log-file. Since the output is created in an infinity-loop, one has to make sure to include a fflush(stdout); after every printf()
Then I can monitor the log-file by using
tail -f /foo/log

Resources