Users trying to run a script as a daemon on a modern system should be using systemd:
[Unit]
Description=hit service
After=network-online.target
[Service]
ExecStart=/path/to/hit.sh
[Install]
WantedBy=multi-user.target
Save this as /etc/systemd/system/hit.service, and then you will be able to start/stop/enable/disable it with systemctl start hit, etc.
Old answer from 2015:
If you'd like to reuse your code sample, it could look something like:
#!/bin/bash
case "$1" in
start)
/path/to/hit.sh &
echo $!>/var/run/hit.pid
;;
stop)
kill `cat /var/run/hit.pid`
rm /var/run/hit.pid
;;
restart)
$0 stop
$0 start
;;
status)
if [ -e /var/run/hit.pid ]; then
echo hit.sh is running, pid=`cat /var/run/hit.pid`
else
echo hit.sh is NOT running
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
Naturally, the script you want to be executed as a service should go to e.g. /usr/local/bin/hit.sh, and the above code should go to /etc/init.d/hitservice.
For each runlevel which needs this service running, you will need to create a respective symlink. For example, a symlink named /etc/init.d/rc5.d/S99hitservice will start the service for runlevel 5. Of course, you can still start and stop it manually via service hitservice start/service hitservice stop
Users trying to run a script as a daemon on a modern system should be using systemd:
[Unit]
Description=hit service
After=network-online.target
[Service]
ExecStart=/path/to/hit.sh
[Install]
WantedBy=multi-user.target
Save this as /etc/systemd/system/hit.service, and then you will be able to start/stop/enable/disable it with systemctl start hit, etc.
Old answer from 2015:
If you'd like to reuse your code sample, it could look something like:
#!/bin/bash
case "$1" in
start)
/path/to/hit.sh &
echo $!>/var/run/hit.pid
;;
stop)
kill `cat /var/run/hit.pid`
rm /var/run/hit.pid
;;
restart)
$0 stop
$0 start
;;
status)
if [ -e /var/run/hit.pid ]; then
echo hit.sh is running, pid=`cat /var/run/hit.pid`
else
echo hit.sh is NOT running
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
Naturally, the script you want to be executed as a service should go to e.g. /usr/local/bin/hit.sh, and the above code should go to /etc/init.d/hitservice.
For each runlevel which needs this service running, you will need to create a respective symlink. For example, a symlink named /etc/init.d/rc5.d/S99hitservice will start the service for runlevel 5. Of course, you can still start and stop it manually via service hitservice start/service hitservice stop
I believe CentOS 7 and above uses systemd. If that is the case for your system, try the following:
Place the script commands you wish to run in
/usr/bin/myscript.Remember to make the script executable with
chmod +x.Create the following file:
/etc/systemd/system/my.service
[Unit]
Description=My Script
[Service]
Type=forking
ExecStart=/usr/bin/myscript
[Install]
WantedBy=multi-user.target
Reload all systemd service files:
systemctl daemon-reloadCheck that it is working by starting the service with
systemctl start my.
Bonus:
For testing the systemd service, it is possible to launch a tmux environment with two window panes, where the top window monitors the output from the script (stdout and stderr) and the bottom window can be used for restarting services. This requires tmux to be installed, then simply:
tmux new-session \; select-layout even-horizontal \; split-window -v journalctl -o cat --since=@$(date +%s) -f -u my \; rotate-window \; set -g status-bg colour0 \; set -g status-fg colour9 \; attach
Then restart the service with:
systemctl restart my
Exit tmux with ctrl-d and then ctrl-c.
Once you're happy with the script and service file, change
Type=forking
into
Type=simple
Done.
I made a .service file to run a shell script, but it doesn't work
Run Shell Script as SystemD Service in Linux - Unix & Linux Stack Exchange
Run a sh file when starting a service (systemd or PM2)
debian - Run Bash Script as Service - Stack Overflow
the startup-scripts.service file:
(it's named like that because I might add more bash scripts to it later)
[Unit] Description=starts a startup sound script [Service] Type=oneshot WorkingDirectory=/home/hananelroe/custom-scripts/start-sound ExecStart=bash /home/hananelroe/custom-scripts/start-sound/startup.sh & ExecStart=bash PID=$! ExecStop=bash kill $PID [Install] WantedBy=multi-user.target
the .sh file:
#!/bin/bash
while true
do
OUTPUT=$(timeout .5 dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'") # timeout .5 terminates the command after .5 second
if echo "$OUTPUT" | grep -q "member=WakeUpScreen"; then
echo "playing starup sound..."
mpg123 ./sound.mp3 > /dev/null 2>&1
fi
donethe .sh script does work if I activate it manually.
to use the service I type:
sudo systemctl daemon-reload sudo service startup-scripts start
startup-scripts.service is located at /usr/lib/systemd/system/
The problem is that, when the script runs as a service, it does not run as "you": it does not have your environment.
Specifically, it does not have your PATH variable.
Either add /home/<usernamehere>/.opam/default/bin to the PATH in your script, or simply hardcode the full path for that program.
The most likely reason is that the directory containing google-drive-ocamlfuse is in the PATH of your login shell, but not in the standard PATH used by systemd.
Just add a line like this at the beginning of your script:
PATH=$PATH:/path/to/google-drive-ocamlfuse
Use su to run the script as a different user:
daemon su -c /home/webreports/report-listen johndoe
where johndoe is the user you want it to run as.
Put the script in /etc/init.d/myservice, then symlink it to /etc/rc.d/S99myservice.
just copying will not do the job. You have to take care the program is decoupled from stdin and stdout. Therefor all output has to be printed to a logfile. You also have to background the program, which should be done by the daemon function.
Hello
I need help please to finish something. (I use Debian 11).
I'm looking for a solution so that an sh file I created, automatically runs when a service (which I also created myself) has finished restarting.
My service is a service that runs a Node.js App. Here it is (with PM2):
module.exports = {
apps: [
{
name: "nextjs_mon-site-prod_1",
cwd: "/home/steph/www/mon-site.com/prod/front-nextjs_1",
script: "npm run start_prod_1", // and then I want to run an sh script
},
],
};I prefer to do this with PM2 (although I'm open to solutions with systemd).
Thanks in advance.
Your .service file should look like this:
[Unit]
Description=Spark service
[Service]
ExecStart=/path/to/spark/sbin/start-all.sh
[Install]
WantedBy=multi-user.target
Now, take a few more steps to enable and use the .service file:
Place it in
/etc/systemd/systemfolder with a name likemyfirst.service.Make sure that your script is executable with:
chmod u+x /path/to/spark/sbin/start-all.shStart it:
sudo systemctl start myfirstEnable it to run at boot:
sudo systemctl enable myfirstStop it:
sudo systemctl stop myfirst
Notes
You don't need to launch Spark with
sudoin your service, as the default service user is already root.Look at the links below for more
systemdoptions.
Moreover
Now what we have above is just rudimentary, here is a complete setup for spark:
[Unit]
Description=Apache Spark Master and Slave Servers
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Service]
User=spark
Type=forking
ExecStart=/opt/spark-1.6.1-bin-hadoop2.6/sbin/start-all.sh
ExecStop=/opt/spark-1.6.1-bin-hadoop2.6/sbin/stop-all.sh
TimeoutSec=30
Restart=on-failure
RestartSec=30
StartLimitInterval=350
StartLimitBurst=10
[Install]
WantedBy=multi-user.target
To setup the service:
sudo systemctl start spark.service
sudo systemctl stop spark.service
sudo systemctl enable spark.service
Further reading
Please read through the following links. Spark is a complex setup, so you should understand how it integrates with Ubuntu's init service.
- https://datasciencenovice.wordpress.com/2016/11/30/spark-stand-alone-cluster-as-a-systemd-service-ubuntu-16-04centos-7/
- https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
- https://www.freedesktop.org/software/systemd/man/systemd.unit.html
Copy-paste this into a terminal (as root) to create /root/boot.sh and run it on boot:
bootscript=/root/boot.sh
servicename=customboot
cat > $bootscript <<EOF
#!/usr/bin/env bash
echo "$bootscript ran at \$(date)!" > /tmp/it-works
EOF
chmod +x $bootscript
cat > /etc/systemd/system/$servicename.service <<EOF
[Service]
ExecStart=$bootscript
[Install]
WantedBy=default.target
EOF
systemctl enable $servicename
To modify the parameters, for example to use a different $bootscript, just set that variable manually and skip that line when copying the commands.
After running the commands, you can edit /root/boot.sh using your favorite editor, and it will run on next boot. You can also immediately run it by using:
systemctl start $servicename