How to auto start script on boot on Raspbian OS Buster? [duplicate] - bash

I now this has been asked before, but I have spent hours and hours trying to find out how to do this and absolutely nothing has worked. I have a python file that I want to automatically run in a terminal window after the pi has booted and loaded its GUI.
I don't know what else to do, and the annoying thing is I had it working for the same project (it took a long time to find out how then as well), but the pi crashed today and I can't remember how to do it again. All I can remember is that I added something to a file ending in /autostart, if that helps.

I was able to launch a python file on startup by running sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart and adding #lxterminal -e python /home/pi/weatherStation/core.py at the end of the file

I had similar issues remembering where and how to do this. Then it all seemed to change again with Buster.
Having just freshly installed Buster onto a new SD card, I had to go through the whole process again. It's definitely easier the third or fourth time ;-)
I found that there was no lxsession folder in the path I had used before:
/home/pi/.config/lxsession/LXDE-pi/autostart
/home/pi/.config/lxsession did not exist in the Buster I just installed (May 2020)
I found a 2020 Buster related article here:
http://wideberry.com/autostart-python-script-after-boot-in-raspbian-buster/
and from that found the autostart file in this path:
/etc/xdg/lxsession/LXDE-pi
Note: this is a root file and will affect all users.
As I had previously found an issue with timings of when to issue the python command, I call a script to pause before running my python scripts.
I added an #lxterminal command in my /etc/xdg/lxsession/LXDE-pi/autostart as follows:
lxpanel --profile LXDE-pi
#pcmanfm --desktop --profile LXDE-pi
#lxterminal -e /home/pi/StartCollectors.sh
#xscreensaver -no-splash
Note: as /etc/xdg/lxsession/LXDE-pi/autostart this is a root protected file, it had to be edited with root powers, e.g. sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
StartCollectors.sh contains this
#!/bin/bash
# started from the /etc/xdg/LXDE-pi/autostart file (reminder)
cd /home/pi/ETA2-copy
echo "starting House Collectors in 20 seconds"
sleep 10
echo "starting House Collectors in 10 seconds"
sleep 10
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 Hiverun.py' -t 'Hive'
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 ETALog.py' -t 'ETA'
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 ETADailySum.py' -t 'Summary'
echo "My work is done. Closing in 10 seconds
sleep 10
exit
the -t options put a recognizable name on the terminal window.
It works.
BTW I collect data from an ETA PU15 boiler over my local Lan. I collect data from my British Gas Hive home control system and the UK Met Office and integrate it into an sqlite table so that I can track the house performance. Why? Why not? Call it a hobby.
I think this is my first answer on this forum. I hope it conforms and that it helps others.

Related

Check if shell script is already running (feh) (i3)

Inside my ~/Pictures/wallpapers/ folder I have a shell script that changes the wallpaper using a while true each time the sleep function terminates:
feh-auto.sh
#!/bin/bash
while true; do
feh /home/maruko/Pictures/wallpapers --randomize --bg-fill
sleep 1800
done
this script is automatically run each time the laptop powers up, or the i3 configuration is reloaded in place using the shortcut Win+Shift+R:
~/.config/i3/config
...
### CHANGE WALLPAPER RANDOMLY
# https://www.linuxandubuntu.com/home/set-background-wallpapers-on-i3wm
exec_always --no-startup-id ~/Pictures/wallpapers/feh-auto.sh
...
Making changes to the i3 configuration is usually accompanied with a reload in place, so that said changes take place.
The problem is that each time a reload occurs, a new instance of feh-auto.sh is also created, meaning that now instead of 1 timer, I have multiple timers of 1800, that will change the wallpaper x times where x is the number of times I reloaded the configuration.
I would like a more appropriate behavior as I reload: check if feh-auto.sh is already running and if it is, do not create a new instance.
Could you please guide me to the best solution?
Thank you.
EDIT
As #balki suggested in the comments, I have created a cron job
(sudo crontab -e)
#reboot /home/maruko/Documents/program-files/shell/feh-auto.sh
to run the following script
feh-auto.sh
#!/bin/bash
export DISPLAY=0:0
feh /home/maruko/Pictures/wallpapers/ --randomize --bg-fill
However, when booting up or rebooting the system, the wallpaper is blank as if nothing really happened. The command sudo systemctl status cronie.service reports the following:
crond[3300]: pam_unix(crond:session): session opened for user root(uid=0) by (uid=0)
CROND[3307]: (root) CMD (/home/maruko/Documents/program-files/shell/feh-auto.sh)
CROND[3300]: (root) CMDOUT (feh ERROR: Can't open X display. It *is* running, yeah?)
CROND[3300]: (root) CMDEND (/home/maruko/Documents/program-files/shell/feh-auto.sh)
CROND[3300]: pam_unix(crond:session): session closed for user root
I don't know what to do next.
The answer to this post is to read documentation:
i3 config comes with the exec and exec_always commands:
exec will execute the command once at boot,
exec_always will execute the command on reloads too.
The solution is to substitute the above call to feh-auto.sh:
~/.config/i3/config
### START FEH WALLPAPER
# note: exec_always will run the script on each reload, not ideal
# --no-startup-id will eliminate the problem of loading icon on boot
exec --no-startup-id /home/maruko/Pictures/wallpapers/feh-auto.sh

xset in BASH script does not work under Cron

To learn about BASH scripting, I set myself the objective to write a Cron script which shuts down a PC with Mint 20 when activity on the Ethernet interface dropped below a threshold over an 1 hour.
I mainly (but not exclusively) use the PC as File/DLNA Server. The script works, but now I find that it also shuts down the PC the rare times I'm using the the front end. So I want my script to verify if the screen has been blanked (As per Power Management settings)
To test the principle I included this in my script:
screenon=$(/usr/bin/xset -q | grep 'Monitor is' | cut -d "s" -f 2)}
which when run in a terminal window gives (debug: set -x)
screenon= On
but when run from cron gives. (logger)
/usr/bin/xset: unable to open display ""
I have learned about similar problems, but cannot figure out how to solve this.
My script includes:PATH=$PATH:/usr/local/bin
and my PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks in advance for any help.

How can I run xautolock as a service?

What I'm trying to do
Okay so I've been trying to create a custom lockscreen on ubuntu using i3lock.
I achieved something I'm comfortable with as a lockscreen, but now I want to use this lockscreen instead of the default gnome one.
So I'm using xautolock for that, which works well when typed in a terminal, but that means I have to enter this manually each time I turn my pc on.
So right now I'm trying to making it a systemd service, and that's where I struggle.
What I block on
First thing is: executing the xautolock command in a shell script doesn't even work.
`#!/bin/bash
`xautolock -time 1 -locker mylock.sh #this line works in CLI but suspend the console
(I've done a chess game on my phone to wait, it lasted way longer than a minute and it didn't do anything whereas executing it in terminal works well)
So there is this problem, but wait there is more!
I've done the .service file, and running mylock.sh from the service returns an exit status=1/FAILURE.
Thus my incomprehension, because the line works in the console, and executing the shell script manually blocks the console aswell so I assume it doesn't fail (it doesn't work as expected tho)
I've tried two different configs for the service:
First one with ExecStart=/usr/bin/xautolock -time 1 -locker <path_to_my_lock>/mylock.sh
Which outputs (code=exited, status=1/FAILURE)
In the other one I tried to executed the a "lockservice.sh" script containing the xautolock line (which has no chance to work given the previous tests):
ExecStart=/bin/bash /PATH/lockservice.sh
Same thing: (code=exited, status=1/FAILURE)
I did a systemctl daemon-reload everytime, I chmoded 755 all the shell scripts. I also tried to execute the service with User=root
What I would like
I basically want something that can replace the gnome default lockscreen with my custom lock script, using xautolock and services isn't mandatory to me, but I'd like not having to run it manually on startup.
Thanks in advance !

Auto Restart SH script on crash?

Hi there guys i have a server running a game I've created and it has three SH scripts that are required to run in separate terminals so what i wanna know is 2 things.
1:is there a way i can get a single script that i double click on and launch all three scripts to where i can see the shell (for Debugging)
2: Is there any way to have said scripts auto restart when they exit or crash? (for full automated access when the server is unattended by a dev)
Server Specs:
6gb ram 60gb SSD 6 core CPU
Ubuntu 14.04
with vnc for desktop control
Here's a SH script for you.
running=1
finish()
{
running=0
}
trap finish SIGINT
while (( running )); do
// Execute the command here that starts your server.
echo "Restarting server on crash.."
sleep 5
done
You can run this script for each server in it's own screen. That way you can see the console output of each one. For example:
screen -S YOURUNIQUENAME -m THESCRIPTABOVE.sh
In order to detach from the screen, hit CTRL + A then CTRL + D. You can get back to the screen by using screen -x YOURUNIQUENAME
For a nice guide on using the screen command, see this article: http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/ . It even has a video to show how it's used.

Bash: Script output to terminal session stops but script finishes normal

I'm opening an ssh-session to a remote server and execute a larger (around 1000 lines) bash-script on the remote machine. It involves several very CPU-intensive calls which run for up to three minutes each. To track the scripts progress it echoes messages placed at several points in the script.
In general the script runs smoothly. From time to time the script runs trough (the resulting file on the remote machine is correct) but the output to the terminal stops. Ctrl-C doesn't help, no prompt, just a frozen session. top in a separate session shows normal execution of the script.
My question: How keep the session alive?
local machine:
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.9
BuildVersion: 13A603
remote machine:
$ lsb_release -d
Description: Ubuntu 12.04.3 LTS
Personally, I would recommend using screen or tmux on the remote terminal for exactly this reason.
Those apps will allow the remote process to continue even if your local SSH session times out.
http://www.bangmoney.org/presentations/screen.html
http://tmux.sourceforge.net/
Start a screen on the remote machine and run your command from it:
screen -S largeScript
And then
./yourLargeScript.sh
Whenever your ssh session gets frozen, you can kill it with ~.
If you ssh again, you can grab back your screen by:
screen -dr largeScript
Make it log to a file instead (perhaps via syslog), and tail that file from wherever is convenient for you. This also helps detach the script so you can run it headless, from a cron job, etc. Also, if the log file has read access for others, they too can monitor it.

Resources