Run shell script on reboot - linux

Tried two methods and neither seem to be working:
crontab -e:
#reboot sleep 60;/home/linuxbox/script.sh
and created a service in /etc/systemd/system/script.service:
[Unit]
Description=a generic service to run on reboot
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/bin/bash /home/linuxbox/script.sh
Type=simple
User=linuxbox
Group=linuxbox
WorkingDirectory=/home/linuxbox
I follow that up with systemctl daemon-reload.
Not sure what is going wrong at this point -- any help is appreciated.

Have you enabled cron?
You can enable and start it with
sudo systemctl enable cron.service

The ExecStart command shouldn't be
/bin/bash /home/linuxbox/script.sh
but should be
/bin/bash -c "/home/linuxbox/script.sh".

Once you've created your script.service unit, you must of course enable it:
systemctl enable script.service
(This might seem obvious, but in qour question you only mention that you run systemctl daemon-reload which is not enough)

Related

Auto-run python script when system is booted on jetson nano

How to auto-run python script made by me when the system is booted on jetson nano?
Step 1: Create a shell file.
sudo nano /usr/local/bin/startup.sh: Type this on the terminal. A new sh file is created. This file consists of the python file that is to be executed. I gave the name startup.sh. It can be any name XYZ.sh
#! /bin/sh: This is called the shebang. This would execute our script using a Bourne shell. This tells the system that the commands in this file should be fed to the interpreter.
sleep 10: This pauses a script for a certain amount of time. He re we pause it for 10 seconds.
In the next line, we insert the code that we use to run the program on the terminal.
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 path/of/the/python/code.py
It looks like this:
#! /bin/sh
sleep 10
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 /home/sooraj/Downloads/incabin-monitoring-system-main/netstreamfeb17.py
Now we close the file using Ctrl+X. and save it.
Step 2: Create a service file
sudo nano /etc/systemd/system/startup.service
Things to write inside it.
[Unit]
Description = INTU_IPC start-uo specific script
[Service]
Type= idle
ExecStartPre = /bin/sleep 10
ExecStart = /usr/local/bin/startup.sh
User=sooraj# write your user name here
[Install]
WantedBy = multi-user.target
Now we close the file using Ctrl+X. and save it.
step 3: Give permission.
sudo chmod +x /usr/local/bin/startup.sh
step 4: enable, start and stop
sudo systemctl enable startup.service
sudo systemctl start startup.service
sudo systemctl stop.service
After starting, to view if it works, we can observe it in the terminal by
journalctl -u startup.service -f
If we edit the service file for the next time, we need to reload it before enabling and starting.
sudo systemctl daemon-reload
sudo systemctl enable startup.service
sudo systemctl start startup.service
Additional information.
systemctl is-enabled startup.service #checks if the service file is enabled.
systemctl is-failed startup.service #checks if the service file failed to start.
systemctl is-active startup.service #checks if the service file is active.
sudo systemctl list-unit-files — type=service #display the list of service files.
Try StartupApplications and add your python execution shell command with proper path.
An even better solution will be to use crontab.
crontab -e
Add #reboot python path/to/script so that the script gets executed every time you reboot.
This link might help you.
As an alternative to systemd or crontab, you can try pm2. It's very easy to configure and use. Just follow a quick start guide. Or just test it the following way:
pm2 start app.py
pm2 save
Note that you should initially generate a startup script to make it work on boot.

Register daemon controllable by start and stop command in Linux

Many system daemon can be started using start/stop command. I was just curious how start/stop works on Linux system. Say I wrote a daemon executable, how should I configure it so that it can be controlled by start/stop in Linux.
I make a daemon in linux (ArchLinux) few years ago, and it works every day perfectly.
There are 2 ways to do this. Short way and long way:
Short Way:
Create a file in /etc/systemd/system/ called for example mydaemon.service :
/etc/systemd/system/mydaemon.service
[Unit]
Description=This is my first daemon! - Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshotmc
RemainAfterExit=yes
ExecStart=/bin/echo -e "Daemon started"
ExecStop=/bin/echo -e "Daemon Stopped"
[Install]
WantedBy=multi-user.target
This service does nothing but show Daemon Started or Stopped. You can change echoes by the sentences you need.
If you need to run some script, try the Long way:
Long way
Create a file in some directory, like root folder or /usr/lib/systemd/scripts called for example
/root/mydaemon.sh
start() {
<your start sentences here
and here>
}
stop() {
<your stop sentences here
and here>
}
case $1 in
start|stop) "$1" ;;
esac
You must to make it runnable (chmod x)
(And you can execute it with start or stop parameter to test it.)
And as second step, create another file in
/usr/lib/systemd/system/mydaemon.service
[Unit]
Description=Second daemon of Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c '/root/mydaemon.sh start'
ExecStart=/bin/echo -e "MyDaemon Started"
ExecStop=/bin/bash -c '/root/mydaemon.sh stop'
ExecStop=/bin/echo -e "MyDaemon Stopped"
[Install]
WantedBy=multi-user.target
Starting and Stopping
systemctl start mydaemon
systemctl stop mydaemon
systemctl status mydaemon
systemctl enable mydaemon
systemctl disable mydaemon
You (and someone) can send me a private msg for help about that.

Fedora 20 how to run script at the end of startup

I am using Fedora 20. I have a two lines bash script needs to be run at the end of the startup. I want it to be run automatically each time when machine is startup. How can I do this?
I tried "sudo crontab -e" to insert my executable script but it always gave me error teling me the the time is not right and cannot modify the file.
You can create a Systemd unit file in /usr/lib/systemd/system/<service_name>.service. Here is a template:
[Unit]
Description=<description_string>
[Service]
WorkingDirectory=<working_directory>
Type=forking
ExecStart=/bin/bash <absolute_path_to_script>
KillMode=process
[Install]
WantedBy=multi-user.target
Replace anything in the angle brackets with your specific information. The 'WantedBy=multi-user.target' is the magic that tells Systemd to run your script on each start.
On the command line, tell Systemd to enable your service:
systemctl enable <service_name>.service
The next time you reboot your script should be run. Logs will be written to /var/log/messages.
Fedora has some basic documentation on unit files: Systemd unit files
You can append /etc/rc.local it runs just after the system starts up.
You may have to create it if doesn't exist:
Check this answer
Charlie's answer is better but you can still use Tiago's answer.
Just don't forget if you want to use /etc/rc.local way, grant execution permission to this file after editing:
chmod +x /etc/rc.local

systemctl testing shell scripts

So I am trying arch linux, but I can't really understand how to make autostart scripts and such. I tried this (I know there are better ways to do it but it's just a test):
this is the service:
[Unit]
Description=testing purposes
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/font
[Install]
WantedBy=multi-user.target
this is the script:
setfont Lat2-Terminus16
I chmod +x'd the script and stuff so I thought it wouldn't make any problems but it does not work unlike what I expected, so I am clearly doing something wrong. Where is the problem?
EDIT: Solved, I changed the ExecStart line to this:
ExecStart=/bin/sh -c '/usr/bin/setfont Lat2-Terminus16'

Programmatically start GPSD daemon in linux

I am doing a project read GPS values output from a GPS dongle and need to
programmatically start the gpsd daemon.
i.e. I need to automate the following command;
sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
I was able to read the coordinates through the code after manually starting the daemon as above. But don't know how to start the daemon through he code.
Since gpsd is a daemon, you can just set the daemon up to be run automatically at startup. How to do this depends on which startup system you have. For example, if you have systemd, you have to write a gpsd.service file, something like this
[Unit]
Description=GPSd daemon service file
[Service]
Type=forking
User=root
Group=dialout
TimeoutStartSec=0
ExecStart=/usr/local/sbin/gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
[Install]
# Grouping mechanism that let systemd start groups of processes up at the same time
WantedBy=multi-user.target
then install it in /lib/systemd/system and finally using the following commands
$ sudo systemctl enable gpsd
$ sudo systemctl start gpsd
the start command is just to run gpsd as systemd daemon without rebooting your system.
for debian its just
dpkg-reconfigure gps

Resources