Skip to content

Instantly share code, notes, and snippets.

@lexmart
Last active July 8, 2023 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexmart/9ac22337c77ef3ce7143fa6e6a51076a to your computer and use it in GitHub Desktop.
Save lexmart/9ac22337c77ef3ce7143fa6e6a51076a to your computer and use it in GitHub Desktop.
Install system service bash script
#!/bin/sh
fullpath=$(realpath $1)
dirpath=$(dirname $fullpath)
name=$(basename $1)
service_file=/etc/systemd/system/$name.service
echo "[Unit]" > $service_file
echo "Description=$name" >> $service_file
echo "After=network.target" >> $service_file
echo "[Service]" >> $service_file
echo "User=$USER" >> $service_file
#echo "Group=www-data" >> $service_file
echo "WorkingDirectory=$dirpath" >> $service_file
echo "ExecStart=$fullpath" >> $service_file
for ((i=2; i<=$#; i++))
do
echo "Environment=\"${!i}\"" >> $service_file
done
echo "[Install]" >> $service_file
echo "WantedBy=multi-user.target" >> $service_file
sudo systemctl daemon-reload
systemctl enable $name.service
systemctl start $name.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment