Skip to content

Instantly share code, notes, and snippets.

@mikkokotila
Last active April 14, 2019 11:42
Show Gist options
  • Save mikkokotila/03072cfa7b6d488541b16ca7db43e23d to your computer and use it in GitHub Desktop.
Save mikkokotila/03072cfa7b6d488541b16ca7db43e23d to your computer and use it in GitHub Desktop.
# name of the service
NAME=$1
# exec and service file paths
EXEC_FILE=$2
SERVICE_FILE=/etc/systemd/system/"$NAME".service
# timing
TIMER_FILE=/etc/systemd/system/"$NAME".timer
TIMING=$3
create_service() {
# create the service file
sudo touch "$SERVICE_FILE"
sudo chmod 664 "$SERVICE_FILE"
# add the content to the service file
sudo echo [Unit] >> "$SERVICE_FILE"
sudo echo Description="$NAME" >> "$SERVICE_FILE"
sudo echo [Service] >> "$SERVICE_FILE"
sudo echo ExecStart=/bin/bash /usr/sbin/"$NAME" >> "$SERVICE_FILE"
sudo echo [Install] >> "$SERVICE_FILE"
sudo echo WantedBy=multi-user.target >> "$SERVICE_FILE"
# create the exec file
echo > /usr/sbin/"$NAME"
sudo chmod +x /usr/sbin/"$NAME"
# add the content to the exec file
echo python "$EXEC_FILE" > /usr/sbin/"$NAME"
# create the timer file
sudo touch "$TIMER_FILE"
sudo chmod 664 "$TIMER_FILE"
# add the content to the timer file
sudo echo [Unit] >> "$TIMER_FILE"
sudo echo Description=Run "$NAME" "$TIMING" >> "$TIMER_FILE"
sudo echo [Timer] >> "$TIMER_FILE"
sudo echo ExecStart=/bin/bash /usr/sbin/"$NAME" >> "$TIMER_FILE"
sudo echo Persistent=true >> "$TIMER_FILE"
sudo echo [Install] >> "$TIMER_FILE"
sudo echo WantedBy=timers.target >> "$TIMER_FILE"
}
create_service
sudo systemctl daemon-reload
# enable the service and the timer
sudo systemtcl enable "$NAME"
sudo systemctl enable "$NAME".timer
# start the service and the timer
sudo systemctl start "$NAME"
sudo systemctl start "$NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment