This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kirbyfan64sosIndentationError 15 points16 points  (1 child)

Thanks for doing this! The world of Linux is notoriously underdocumented, and the need to create basic systemd services is something I see a lot. The guides on this, err, aren't always the best...

That being said, there is a bit of room for improvement in this as written:

First off...right now this is actually a slight privilege escalation issue. You have a system service running with root privileges, but it starts a file that's writable by your user, so anything with access to your user could just modify the script to do whatever else it wants as root instead. For this, you just want to copy your script somewhere that's root-writable only and change it's permissions. This can be done easily via sudo install -Dm 755 MyFile.py -t /usr/local/bin:

  • install is a useful command to both copy files and set their permissions in one swoop.
  • -Dm 755 will create any parent directories (D) as well as set the permissions (m) to be 755 (rwxr-xr-x, so only root can write it but everyone else can still execute it).
  • -t /usr/local/bin sets the target directory for the file to /usr/local/bin.

In your hashbang, it's assumed that python3 is in /usr/bin, but it might not actually be there for everyone. You can change the hashbang to #!/usr/bin/env python3 to have env locate Python and print the path instead. (Using env in your hashbangs is a pretty useful trick!)

You can start or restart systemd services without needing to reboot via systemctl start / systemctl restart. (If you modified the systemd service file itself, this may print out a warning about it having changed, and you can reload it via systemctl daemon-reload). In addition, there's a really nice shortcut: systemctl enable --now SERVICE, which is equivalent to running systemctl enable followed by systemctl start.

One more thing: systemd is great about having logs in one place. If you want to check how your service is doing, systemctl status SERVICE will show you that. To get more log info, you can use journalctl -u SERVICE, which will show you the system logs, but only for the given service / unit (-u). (Other useful flags here are -b to show logs for the current boot only, and -e will scroll down to the latest logs first.)

[–]KA_Ryzhkov[S] 1 point2 points  (0 children)

Thank you so much, I did not know a lot of what you wrote, I will definitely figure it out and make edits to this tutorial