Systemd reload process gets killed - linux

My systemd unit file looks something like this
start() {
java -jar server.jar &
}
reload() {
#do some application specific reload stuff
start
}
I'm realizing that right when the systemd call to reload finishes, the process running java -jar is actually dead. Systemd seems to think that my reload process is done and kills it. Is there any way to configure and cancel this process killing behavior? I have tried disowning the process, and messing with KillMode=blah and Type=blah in the service file, but no result.
Running on CentOS 7.4

I don't know if I understood your question, do you want the systemd unit file for executing server.jar, right?
Try it by following sequences.
First create your blah service unit file.
# vim /etc/systemd/system/blah.service
[Unit]
Description=blah service
After=network.target
Requires=network.target
[Service]
Type=simple
EnvironmentFile=/etc/sysconfig/blah
ExecStart=/usr/bin/java -jar server.jar
Restart=always
User=blah_USERNAME
[Install]
WantedBy=multi-user.target
And reload the blah service unit file.
# systemctl daemon-reload
Test it!
# systemctl start blah
# systemctl status blah
# systemctl restart blah
# systemctl status blah
I hope this will help you.

Related

How to send `SIGINT` over `systemctl` to custom daemon/service

I have a script that I would like to run as service on my Linux machine, let's call it my_script.sh. It is executable and can be run over ./path/to/my_scrit.sh.
I also have script that kills my_script.sh, let's call it kill_my_scprit.sh. The contents of kill_my_script.sh are:
#!/bin/bash
sudo kill -SIGINT $(pgrep my_script)
In essence, this should mimic Ctrl+C for my_script.sh.
This works perfectly fine if I run the scripts from a terminal, i.e. in ttyX I run ./path/to/my_scrit.sh to start it and in ttyY ./path/to/kill_my_scrit.sh to initiate the shut down sequence of my_script.sh.
As mentioned before, the goal is to run this as deamon, so I created /etc/systemd/system/my_script.service
[Unit]
....
[Service]
Type=simple
User=root
WorkingDirectory=/path/to/dir/
ExecStart=/path/to/my_script.sh
ExecStop=/path/to/kill_my_script.sh
Restart=on-failure
[Install]
...
Using sudo systemctl start my_script.service starts the script as expected, but using sudo systemctl stop my_script.service starts the shutdown sequence for my_script.sh, but doesn't let it finish... Am I missing something? The shutdown sequence takes roughly 10sec...

Run shell script on reboot

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)

How to Disable logstash error logging in syslog

All Logstash errors logs in /var/log/logstash/* , but all the error logs of Logstash logs in /var/log/syslog too.
Is there any way to disable logging errors of Logstash in /var/log/syslog?
I suppose you run logstash from systemd. For unknown reason logstash print it's logs on stdout/stderr, so journald is forwarding console output to syslog.
Redirect stdout/err in systemd unit file like this:
[Service]
Type=simple
Restart=always
WorkingDirectory=/data/logstash
ExecStart=/usr/bin/sudo -u logstashuser bash -c "/opt/logstash/bin/logstash --path.settings /opt/logstash/conf >/dev/null 2>/logs/logstash/logstash.error.log"
then
systemctl daemon-reload
and
systemctl stop logstash.service
systemctl start logstash.service
Another solution is to modify /etc/systemd/system/logstash.service and change those two option as following
StandardOutput=null
StandardError=null
# cat /etc/systemd/system/logstash.service
[Unit]
Description=logstash
[Service]
Type=simple
User=logstash
Group=logstash
# Load env vars from /etc/default/ and /etc/sysconfig/ if they exist.
# Prefixing the path with '-' makes it try to load, but if the file doesn't
# exist, it continues onward.
EnvironmentFile=-/etc/default/logstash
EnvironmentFile=-/etc/sysconfig/logstash
ExecStart=/usr/share/logstash/bin/logstash "--path.settings" "/etc/logstash"
StandardOutput=null
StandardError=null
Restart=always
WorkingDirectory=/
Nice=19
LimitNOFILE=16384
# When stopping, how long to wait before giving up and sending SIGKILL?
# Keep in mind that SIGKILL on a process can cause data loss.
TimeoutStopSec=infinity
[Install]
WantedBy=multi-user.target
Then of course
#systemctl daemon-reload
#systemctl restart logstash

Running bash script as a service and write to another bash script is not working

I have the following problem using bash script.
Here is what I have inside the 'startup' script file:
#!/bin/bash
java -cp ../lib/online-store.jar:../lib/* com.online.store.Main
OnlineStorePID=$!
if [$OnlineStorePID -ne 0] then
echo "kill $OnlineStorePID" > shutdown
fi
Basically what I do, is to run a java application, get the process id and write it to another bash file. All this process works when I execute the startup script, and the 'shutdown' script file is updated successfully with a line containing 'kill processIDNumber' cmd.
Now I have tried to create a service on Ubuntu for this script using the following commands:
sudo systemctl daemon-reload
sudo systemctl enable online-store.service
sudo systemctl start online-store
When I start the service the java application starts successfully, but the shutdown script file is not updated. It seems that the 'echo "kill $OnlineStorePID" > shutdown' line is not executed. I don't get any complain errors. Does anyone knows what's the problem here.
Here is my service file:
[Unit]
Description=Online store service
Requires=multi-user.target
After=multi-user.target
Wants=mysql.service
[Service]
WorkingDirectory=/home/user/Desktop/online-store-service
#path to executable.
ExecStart=/home/user/Desktop/online-store-service/bin/startup
ExecStop=/home/user/Desktop/online-store-service/bin/shutdown
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Change your script and run the java command like below as back ground process
java -cp ../lib/online-store.jar:../lib/* com.online.store.Main >/dev/null 2>&1 &

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.

Resources