Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


CentOS System Service

08 Jun 2020 » linux

/usr/lib/systemd/system/<name>.service

[Unit]
Description = <description>
After = network.target

[Service]
#Environment="foo=bar"
WorkingDirectory=<working dir>
Restart=on-failure
#RestartSec=3
ExecStart=/usr/bin/python3 app.py

[Install]
WantedBy = multi-user.target
systemctl daemon-reload
systemctl start|restart|enable your_service
systemctl list-units
UNIT                                      LOAD   ACTIVE SUB     DESCRIPTION
atd.service                               loaded active running ATD daemon
avahi-daemon.service                      loaded active running Avahi mDNS/DNS-SD Stack
dbus.service                              loaded active running D-Bus System Message Bus
dcron.service                             loaded active running Periodic Command Scheduler
dkms.service                              loaded active exited  Dynamic Kernel Modules System
getty@tty1.service                        loaded active running Getty on tty1
systemctl list-unit-files
UNIT FILE                                  STATE   
proc-sys-fs-binfmt_misc.automount          static  
dev-hugepages.mount                        static  
dev-mqueue.mount                           static  
proc-fs-nfsd.mount                         static  
proc-sys-fs-binfmt_misc.mount              static  
sys-fs-fuse-connections.mount              static  
sys-kernel-config.mount                    static  
sys-kernel-debug.mount                     static  
tmp.mount                                  static  
var-lib-nfs-rpc_pipefs.mount               static  
org.cups.cupsd.path                        enabled
svcname=${svcname2:-}
if [ ! "$svcname" == "" ]; then
filename="/usr/lib/systemd/system/${svcname}.service"
echo "to create $filename"
cat <<EOF > $filename
[Unit]
Description = <description>
After = network.target

[Service]
#Environment="foo=bar"
WorkingDirectory=<working dir>
Restart=on-failure
#RestartSec=3
ExecStart=/usr/bin/python3 app.py

[Install]
WantedBy = multi-user.target
EOF
echo "file created"
echo $filename

fi
svcname=${svcname2:-}
if [ ! "$svcname" == "" ]; then
filename="Makefile"
echo "to create $filename"
cat <<EOF > $filename


svc=$svcname

restart:
$(echo -e "\tsystemctl restart \$(svc)")

start:
$(echo -e "\tsystemctl start \$(svc)")

stop:
$(echo -e "\tsystemctl stop \$(svc)")

status:
$(echo -e "\tsystemctl status \$(svc)")

enable:
$(echo -e "\tsystemctl enable \$(svc)")

edit:
$(echo -e "\tvim /usr/lib/systemd/system/\$(svc).service")
$(echo -e "\tsystemctl daemon-reload")

dump:
$(echo -e "\tcat /usr/lib/systemd/system/\$(svc).service > \$(svc).service")

restore:
$(echo -e "\t[ -f \$(svc).service ] &&  cp \$(svc).service  /usr/lib/systemd/system/\$(svc).service")

logs:
$(echo -e "\tjournalctl -u \$(svc) -f")



EOF

fi

test2

svcname=${svcname2:-}
if [ ! "$svcname" == "" ]; then
filename="Makefile"
echo "to create $filename"
cat <<EOF > $filename


svc=$svcname
backup="W1VuaXRdCkRlc2NyaXB0aW9uID0gPGRlc2NyaXB0aW9uPgpBZnRlciA9IG5ldHdvcmsudGFyZ2V0CgpbU2VydmljZV0KI0Vudmlyb25tZW50PSJmb289YmFyIgpXb3JraW5nRGlyZWN0b3J5PTx3b3JraW5nIGRpcj4KUmVzdGFydD1vbi1mYWlsdXJlCiNSZXN0YXJ0U2VjPTMKRXhlY1N0YXJ0PS91c3IvYmluL3B5dGhvbjMgYXBwLnB5CgpbSW5zdGFsbF0KV2FudGVkQnkgPSBtdWx0aS11c2VyLnRhcmdldAo="


restart:
$(echo -e "\tsystemctl restart \$(svc)")

start:
$(echo -e "\tsystemctl start \$(svc)")

stop:
$(echo -e "\tsystemctl stop \$(svc)")

status:
$(echo -e "\tsystemctl status \$(svc)")

enable:
$(echo -e "\tsystemctl enable \$(svc)")

edit:
$(echo -e "\tvim /usr/lib/systemd/system/\$(svc).service")
$(echo -e "\t\$(backupfn)")
$(echo -e "\tsystemctl daemon-reload")

dump:
$(echo -e "\tcat /usr/lib/systemd/system/\$(svc).service > \$(svc).service")

define backupfn =
$(echo -e "\tcodeb64=\$\$(base64 /usr/lib/systemd/system/\$(svc).service -w0)")
$(echo -e "\tsed -i \"s/^[b]ackup=.*/backup=\\\"\$\$codeb64\\\"/\" Makefile")
endef

        
restore:
$(echo -e "\t[ -f \$(svc).service ] \\")
$(echo -e "\t&& cp \$(svc).service /usr/lib/systemd/system/\$(svc).service \\")
$(echo -e "\t|| echo \"\$(backup)\" | base64 -d > /usr/lib/systemd/system/\$(svc).service")   
        
logs:
$(echo -e "\tjournalctl -u \$(svc) -f")


.ONESHELL:

EOF

fi

Related Posts