Difference between revisions of "Systemd service file"

From Teknologisk videncenter
Jump to: navigation, search
(Created page with "If i want a service daemon to log if my server is online every minute. The following procedure could be used: ==My service== <source lang=bash> #!/bin/bash # Filename: /usr/...")
 
m
Line 1: Line 1:
 
If i want a service daemon to log if my server is online every minute. The following procedure could be used:
 
If i want a service daemon to log if my server is online every minute. The following procedure could be used:
 
==My service==
 
==My service==
 
 
 
<source lang=bash>
 
<source lang=bash>
 
#!/bin/bash
 
#!/bin/bash
Line 21: Line 19:
 
         sleep 60
 
         sleep 60
 
done
 
done
 +
</source>
 +
==My service file==
 +
<source lang=bash>
 +
# Filename: /lib/systemd/system/mysserver-onlined.service
 +
# Owner: root:root
 +
# Permissions: 644
 +
[Unit]
 +
Description=Test if my server online service
 +
After=network.target
 +
 +
[Service]
 +
ExecStart=/usr/local/sbin/mysserver-online.sh
 +
Type=notify
 +
Restart=always
 +
 +
 +
[Install]
 +
WantedBy=default.target
 +
RequiredBy=network.target
 
</source>
 
</source>

Revision as of 06:51, 17 April 2024

If i want a service daemon to log if my server is online every minute. The following procedure could be used:

My service

#!/bin/bash
# Filename: /usr/local/sbin/mysserver-online.sh
# Ownership: root:root 
# Permissions:  755 
IP="192.168.1.72"
LOGFILE="/tmp/myserver.log"

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 online service
After=network.target

[Service]
ExecStart=/usr/local/sbin/mysserver-online.sh
Type=notify
Restart=always


[Install]
WantedBy=default.target
RequiredBy=network.target