Difference between revisions of "Systemd service file"
From Teknologisk videncenter
m (→My service-file) |
m (→Need to know commands) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
#!/bin/bash | #!/bin/bash | ||
# Filename: /usr/local/sbin/mysserver-online.sh | # Filename: /usr/local/sbin/mysserver-online.sh | ||
− | # | + | # Owner: root:root |
− | # Permissions: | + | # Permissions: 755 |
+ | |||
IP="192.168.1.72" | IP="192.168.1.72" | ||
LOGFILE="/tmp/myserver.log" | LOGFILE="/tmp/myserver.log" | ||
+ | export LOGFILE | ||
+ | |||
+ | # Example: Bash catches(trap) exit signal and runs cleanup function (Not necessary) | ||
+ | # $LOGFILE used in trap routine - needs to be exported | ||
+ | cleanup() { | ||
+ | echo "$(date): Cleaning up" | tee -a $LOGFILE | ||
+ | } | ||
+ | trap 'cleanup' EXIT | ||
+ | |||
+ | |||
+ | |||
+ | echo "$(date): Starting service" | tee -a $LOGFILE | ||
while : | while : | ||
Line 13: | Line 26: | ||
if ping -c 1 $IP > /dev/null 2>&1 | if ping -c 1 $IP > /dev/null 2>&1 | ||
then | then | ||
− | echo "$(date) online" >> $LOGFILE | + | echo "$(date): online" >> $LOGFILE |
else | else | ||
− | echo "$(date) OFFLINE" >> $LOGFILE | + | echo "$(date): OFFLINE" >> $LOGFILE |
fi | fi | ||
sleep 60 | sleep 60 | ||
done | done | ||
</source> | </source> | ||
+ | |||
==My service-file== | ==My service-file== | ||
<source lang=bash> | <source lang=bash> | ||
Line 30: | Line 44: | ||
[Service] | [Service] | ||
− | ExecStart=/usr/local/sbin/mysserver- | + | ExecStart=/usr/local/sbin/mysserver-online.sh |
Type=simple | Type=simple | ||
Restart=always | Restart=always | ||
Line 65: | Line 79: | ||
==Enable service-file== | ==Enable service-file== | ||
Enabling the service file makes it a part of '''systemd''' | Enabling the service file makes it a part of '''systemd''' | ||
− | If it is a service and should be running all the time - do '''shutdown -r now''' and check with '''systemctl status | + | If it is a service and should be running all the time - do '''shutdown -r now''' and check with '''systemctl status myserver-onlined''' that it is running |
<source lang=bash> | <source lang=bash> | ||
− | sudo systemctl enable | + | sudo systemctl enable myserver-onlined.service |
</source> | </source> | ||
Line 73: | Line 87: | ||
<source lang=bash> | <source lang=bash> | ||
# Check the service status | # Check the service status | ||
− | sudo systemctl status myserver- | + | sudo systemctl status myserver-onlined |
# Start the service manually | # Start the service manually | ||
− | sudo systemctl start myserver- | + | sudo systemctl start myserver-onlined |
# Stop the service manually | # Stop the service manually | ||
− | sudo systemctl stop myserver- | + | sudo systemctl stop myserver-onlined |
# Restart the service manually | # Restart the service manually | ||
− | sudo systemctl restart myserver- | + | sudo systemctl restart myserver-onlined |
# If you make changes to the service file - reload | # If you make changes to the service file - reload |
Latest revision as of 11:24, 17 April 2024
If i want a service daemon to log if my server is online every minute. The following procedure could be used:
Contents
My service
#!/bin/bash
# Filename: /usr/local/sbin/mysserver-online.sh
# Owner: root:root
# Permissions: 755
IP="192.168.1.72"
LOGFILE="/tmp/myserver.log"
export LOGFILE
# Example: Bash catches(trap) exit signal and runs cleanup function (Not necessary)
# $LOGFILE used in trap routine - needs to be exported
cleanup() {
echo "$(date): Cleaning up" | tee -a $LOGFILE
}
trap 'cleanup' EXIT
echo "$(date): Starting service" | tee -a $LOGFILE
while :
do
if ping -c 1 $IP > /dev/null 2>&1
then
echo "$(date): online" >> $LOGFILE
else
echo "$(date): OFFLINE" >> $LOGFILE
fi
sleep 60
done
My service-file
# Filename: /lib/systemd/system/mysserver-onlined.service
# Owner: root:root
# Permissions: 644
[Unit]
Description=Test if my server is online
After=network.target
[Service]
ExecStart=/usr/local/sbin/mysserver-online.sh
Type=simple
Restart=always
[Install]
WantedBy=default.target
Enabling the service
Copy files to destination
Make sure your service and your service-file are in the right destination.
sudo cp myserver-online.sh /usr/local/sbin
sudo chown root:root /usr/local/sbin/myserver-online.sh
sudo chmod 755 /usr/local/sbin/myserver-online.sh
sudo cp myserver-onlined.service /lib/systemd/system
sudo chown root:root /lib/systemd/system/myserver-onlined.service
sudo chmod 644 /lib/systemd/system/myserver-onlined.service
Reload system file
systemd reloads and hopefully finds your new service-file
sudo systemctl daemon-reload
Verify files
systemd-analyze checks the service-file for errors. (Correct errors if any - remember to reload system files)
sudo systemd-analyze verify myserver-onlined.service
Enable service-file
Enabling the service file makes it a part of systemd If it is a service and should be running all the time - do shutdown -r now and check with systemctl status myserver-onlined that it is running
sudo systemctl enable myserver-onlined.service
Need to know commands
# Check the service status
sudo systemctl status myserver-onlined
# Start the service manually
sudo systemctl start myserver-onlined
# Stop the service manually
sudo systemctl stop myserver-onlined
# Restart the service manually
sudo systemctl restart myserver-onlined
# If you make changes to the service file - reload
sudo systemctl daemon-reload