diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-20 13:55:05 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-20 13:55:05 +0300 |
| commit | 723cce0f413ff3984b8605e56090987a67834b5c (patch) | |
| tree | 19ecb7bff9c4dbecef6ee089719e31dfbcdda66f /systemd-service-create.py | |
| parent | 3c01b8f5d01df345f32c21a86b8e7fe0db90ce01 (diff) | |
| download | RepoSync-723cce0f413ff3984b8605e56090987a67834b5c.tar RepoSync-723cce0f413ff3984b8605e56090987a67834b5c.tar.gz RepoSync-723cce0f413ff3984b8605e56090987a67834b5c.zip | |
Added a script for creating a systemd service and systemd timer files; Slightly improved some code in main script
Diffstat (limited to 'systemd-service-create.py')
| -rwxr-xr-x | systemd-service-create.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/systemd-service-create.py b/systemd-service-create.py new file mode 100755 index 0000000..4527bd4 --- /dev/null +++ b/systemd-service-create.py @@ -0,0 +1,48 @@ +#!/bin/python3 +import os + +# This tests root permissions by trying to write a file (it deletes it afterwards) +# "Easer to Ask Forgiveness than permission" https://stackoverflow.com/a/2806932/12036073 +try: + test = open(r"/etc/test", "w") + test.close(); + os.remove(r"/etc/test") +except PermissionError as e: + exit("You must run this script as root!") + +cwd = os.getcwd() + +if "home" in cwd: + print("Warning: you are running this script from the home directory! If you use SELinux, the service won't work (https://serverfault.com/a/957087/592409)!") + +service_file = open(r"/etc/systemd/system/reposync.service", "w") +service_file.write( +f"""[Unit] +Description=Python script for synchronizing repositories + +[Service] +Type=oneshot +WorkingDirectory={cwd} +ExecStart={cwd}/reposync.py +""") +service_file.close() + +timer_file = open(r"/etc/systemd/system/reposync.timer", "w") +timer_file.write( +f"""[Unit] +Description=Python script for synchronizing repositories + +[Timer] +OnBootSec=10min +OnUnitActiveSec=10min + +[Install] +WantedBy=timers.target +""") +timer_file.close() + +print( +"""reposync.service and reposync.timer files created successfully! +You'll now need to run the following to activate them (as root): systemctl daemon-reload && systemctl start reposync.timer +To remove them, execute (as root) this: rm -f /etc/systemd/system/reposync.* +""") |
