Systemd: Difference between revisions

From Anthony Pastor Wiki Notes - Verba volant, scripta manent
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 55: Line 55:
For complete details, see Fedora's documentation on how to write a systemd script.
For complete details, see Fedora's documentation on how to write a systemd script.


Example:
<u>Example:</u>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">

Latest revision as of 11:05, 11 May 2016

Configure a Bash script to run as a service

$ cat /etc/systemd/system/my-bash-script.service 

[Unit]
Description=Basic Script Example
After=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/home/my-script/my-script.sh

[Install]
WantedBy=multi-user.target


systemctl enable my-bash-script


Disable Systemd log redirection (useful to debug)

_SYSTEMCTL_SKIP_REDIRECT=true bash -x /etc/init.d/mysql start


To pass variable to a Systemd service as we used to do with Sysvinit via /etc/default/mydefaulsparameters file

(source: http://serverfault.com/a/413408)

The current best way to do this is:

Create a directory /etc/systemd/system/myservice.service.d, and inside that directory create a file whose name ends in .conf, and in this file you can add to or override any part of the unit shipped by the distribution.

For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:

[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"

Also note that if the directory exists and is empty, your service will be disabled! If you don't intend to put something in the directory, ensure that it does not exist.

For reference, the old way was:

The recommended way to do this is to create a file /etc/sysconfig/myservice which contains your variables, and then load them with EnvironmentFile.

For complete details, see Fedora's documentation on how to write a systemd script.

Example:

$ cat /etc/systemd/system/kafka.service.d/defaults.conf
Environment="KAFKA_JMX_OPTS="
Environment="-server -XX:+UseCompressedOops -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -XX:+DisableExplicitGC -Djava.awt.headless=true"
Environment="$KAFKA_JVM_PERFORMANCE_OPTS $KAFKA_JMX_OPTS"