Let systemd manage your running of unprivileged scripts

Instructions


let's save a text file named "test.sh" having the following content


#! /bin/bash
for i in `seq 50`
do
   echo $i
   sleep 1
done


it just print number from 1 to 50 second by second (you can change 50 to any number) now as regular user type


chmod +x ./test.sh
systemd-run --user --unit=my-test ./test.sh 

the above command will run the script as a user service called my-test

at any time you can trace it with systemctl and see the logs using journalctl like this


journalctl -ln 100 -f --user-unit=my-test
systemctl --user status my-test

you can abort it using


systemctl --user stop my-test



Use cases


Let's assume you have a web interface that trigger something and you want to trace it later
just make your unprivileged web application (written in php/python and running as regular non-root user) called "systemd-run --user" and query the status and follow the logs using systemctl and journactl

for example if you have a web interface that triggers building something, running a database migration or indexing something, deploying some ansible playbook ...etc. 

  • you can check if it's running
  • you can start a new job
  • you can monitor the status
  • you can follow the logs
  • you can get real time notification using dbus

Comments

Popular posts from this blog

Making minimal graphical operating system

Bootstrapping Alpine Linux QCow2 image

DIY Docker using Skopeo+OStree+Runc