systemctl service not starting after system restart - linux

I have this service which I want to be able to start as a service on system restart. I am using Ubuntu 15.10. The service configuration file looks like this:
[Unit]
Description=Service client
After=syslog.target
[Service]
ExecStart=/bin/bash -c "/usr/local/bin/service_clientd start"
ExecStop=/bin/bash -c "/usr/local/bin/service_clientd stop"
Type=simple
[Install]
WantedBy=multi-user.target
The service starts perfectly with systemctl command, but does not start automatically after system restart.

Do this:
systemctl enable servicename.service
https://wiki.archlinux.org/index.php/systemd#Using_units

Related

Add dependency between two systemd services to start again if one of them is killed

I have two systemd services A.service and B.service in /etc/systemd/system/ directory.
Below is the configuration in A.service
[Unit]
Description=A Service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/A_service.sh -a start
ExecStop=/opt/myapp/A_service.sh -a stop
[Install]
WantedBy=multi-user.target
Below is the configuration in B.service
[Unit]
Description=B Service
After=A.service
BindsTo=A.service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/B_service.sh -a start
ExecStop=/opt/myapp/B_service.sh -a stop
[Install]
WantedBy=multi-user.target
I want to create a dependency such that when A.service gets restarted B.service should also get restarted.
But it's not working when i kill A.service with sudo kill -9 <A.service pid>.
I can see A.service getting restarted after kill but B.service is in stopped state, not getting restarted.
Can someone help me configure this dependency?
Thanks in Advance.
You want to use the PartOf dependency.
https://www.freedesktop.org/software/systemd/man/systemd.unit.html#PartOf=
PartOf=
Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.
When PartOf=b.service is used on a.service, this dependency will show as ConsistsOf=a.service in property listing of b.service. ConsistsOf= dependency cannot be specified directly.
Description=B Service
After=A.service
BindsTo=A.service
PartOf=A.service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/B_service.sh -a start
ExecStop=/opt/myapp/B_service.sh -a stop
[Install]
WantedBy=multi-user.target

Auto-starting Twonky Server on Ubuntu 18.04 using systemd

I was trying to set up a Twonky Server on Ubuntu. The server works fine, but I could not get systemd to autostart the server (using a service file I created at /etc/systemd/system/twonkyserver.service). Sometimes I got the cryptic error message that some PID-file (/var/run/mediaserver.pid) is not accessible, the exit code of the service is 13, which apparently is a EACCES Permission denied error . The service runs as root.
I finally managed to fix the problem by setting PIDFile in the twonkyserver.service file to /var/run/mediaserver.pid. For reference, find the service file below:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
As described above, the below service file auto-starts the Twonky Server on boot. Simply create it using vim /etc/systemd/system/twonkyserver.service. This assumses that you have installed the Twonky Server to usr/local/twonky. The shell-file twonky.sh already provides a nice interface to the service file (twonky.sh start|stop|reload|restart, also see twonky.sh -h).
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
I would slightly amend the start and stop commands from twonky.sh and put them directly into the twonky.service file for systemd:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
#Systemd will ensure RuntimeDirectory for the PID file is created under /var/run
RuntimeDirectory=twonky
PIDFile=/var/run/twonky/mediaserver.pid
# use the -mspid argument for twonkystarter to put the pid file in the right place
ExecStart=/usr/local/twonky/twonkystarter -mspid /var/run/twonky/mediaserver.pid -inifile /usr/local/twonky/twonkyserver.ini -logfile /usr/local/twonky/twonky.log -appdata /usr/local/twonky
ExecStop=kill -s TERM $MAINPID
ExecStopPost=-killall -s TERM twonkystarter
ExecStopPost=-killall -s TERM twonky
# Twonky 8.5.1 doesn't reload, it stops instead (on arm at least)
# ExecReload=kill -s HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
You need to be sure the paths in the ExecStart command match where you unpacked twonky, and also where you want the .pid file, configuration, logfile and runtime appdataunless you are happy with their default locations.
After putting that all into/etc/systemd/system/twonky.server, run
sudo systemctl daemon-reload
sudo systemctl start twonky
sudo systemctl enable twonky

Daemon service in systemd

I have managed to install daemon service in /etc/systemd/system, however I am not sure about 2 things:
Whether the daemon services should reside there
How can I elegantly check whether a daemon service is installed or not in systemd?
1.If the daemon services should reside there
yes, it is the .service location. The file that you should put here is:
mydeamon.service
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=**YourUser**
ExecStart=**pathToYourScript**
[Install]
WantedBy=multi-user.target
You’ll need to:
set your actual username after User=
set the proper path to your script in ExecStart= (usually /usr/bin/ You can put your script here)
creating-a-linux-service-with-systemd
2.How can I elegantly check if a daemon service is installed or not in systemd?
systemctl has an is-active subcommand for this:
systemctl is-active --quiet service
will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts:
systemctl is-active --quiet service && echo Service is running
test Service is running

how to reload a pythonic service on centos 7?

I have a python app that I made it as a service on centos 7.
I created a file in /usr/lib/systemd/system with my project name. And wrote these on it:
[Unit]
Description=My Script Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python3.6 /usr/src/python-project/sampleService-services/serverprotocol.py
[Install]
WantedBy=multi-user.target
After that:
$ sudo systemctl daemon-reload
$ sudo systemctl enable sampleService.service
$ sudo reboot
I can start, restart and stop this service with commands:
$ systemctl start sampleService.service
$ systemctl restart sampleService
$ systemctl stop sampleService
But when i try to reload it with these commands:
$ systemctl reload sampleService
or
$ service sampleService reload
I get this error:
Failed to reload sampleService.service: Job type reload is not applicable for unit basiscore.service.
See system logs and 'systemctl status sampleService.service' for details.
Is there any command for reload this pythonic service ?!
how can I reload my service without restarting it ?!
Under the ExecStart= line, try to add
Restart=on-failure
RestartSec=10s
For systemctl reload ... to work, you need to provide an ExecReload= line in your unit (service) file. A common example is:
ExecReload=/bin/kill -HUP $MAINPID
That requires your program to catch and act on a SIGHUP signal. If your application has a different mechanism to trigger a reload of its configuration while running, then provide some other suitable command which generates that trigger.

Cannot start systemd service

I have a spring boot executable jar in a digital ocean droplet. I'm able to execute the jar using java -jar myapp.jar Now I want to have i run as a service.
I've created the file /etc/systemd/system/myapp.service with these contents
[Unit]
Description=myapp
After=syslog.target
[Service]
User=kevin
ExecStart=/var/myapp/myapp-backend-1.0-SNAPSHOT.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
Then enabled it to start at system startup
systemctl enable myapp.service
I'm now attempting to start the service
systemctl start myapp.service
But I'm getting this error
Failed to start myapp.service: Unknown unit: myapp.service
See system logs and 'systemctl status myapp.service' for details.
running systemctl status myapp.service return this:
Failed to get properties: No such interface ''
Try this :
[Unit]
Description=myapp
After=syslog.target
[Service]
User=kevin
ExecStart=java -jar /var/myapp/myapp-backend-1.0-SNAPSHOT.jar
SuccessExitStatus=143
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
I have add :
java -jar in youre ExecStart
Restart=always => If java crack, systemd restart the service
RestartSec=5 => After crash the service restart avec 5 seconds
After youre modification, reload the systemd daemon :
systemctl daemon-reload
Enable on startup :
systemctl enable myapp.service
And start now :
systemctl start myapp.service
You need a wrapper script for the jar mentioned in ExecStart to handle start, stop and restart methods.
Extensive instructions and an example script can be found here

Resources