How to Control a User Systemd Using 'Systemctl --User' After Sudo Su - Myuser

How to add user input when starting a service in systemd

I figured it out in such a way:
I created .sh file in usr/bin with this content:

#!/usr/bin/bash
yes | /home/marek/webcash/webminer

Then I created config file in systemd with ExecStart: /path/to/file.sh

and now it works - systemd is running correctly, the logs are logging, the answer "yes" was typed only once in binary file when the user prompt appeared.

Start systemd user service from Rails

I was able to solve the issue by telling the shell where to find the correct DBUS for the user. I set the environment variable XDG_RUNTIME_DIR to the location of DBUS for the user, 1001 in my case. Check echo $UID to find user id, I couldn't use this inline.

`export XDG_RUNTIME_DIR="/run/user/1001" && systemctl --user start myservice`

Thanks to D.j. Molny for the initial sign post and NeilCasey for the remainder

Ansible systemctl --user for another user

Both options are feasible.

1) remote_user: bob

- hosts: test_01
become: no
remote_user: admin
tasks:
- command: whoami
register: result
- debug:
var: result.stdout
- command: whoami
remote_user: bob
register: result
- debug:
var: result.stdout

gives:

"result.stdout": "admin"
"result.stdout": "bob"

2) pipelining = true quoting from Becoming an Unprivileged User

Use pipelining. When pipelining is enabled, Ansible doesn’t save the module to a temporary file on the client. Instead it pipes the module to the remote python interpreter’s stdin. Pipelining does not work for python modules involving file transfer (for example: copy, fetch, template), or for non-python modules.

- hosts: test_01
become: no
remote_user: admin
tasks:
- command: whoami
register: result
- debug:
var: result.stdout
- command: whoami
become_user: bob
become_method: sudo
become: yes
register: result
- debug:
var: result.stdout

gives

"result.stdout": "admin"
"result.stdout": "bob"

with

$ grep pipe ansible.cfg 
pipelining = true

Can non-sudo user add systemd units?

You can't allow a user to start a service as root (or as any other user), but users can start units in with user bus, just place them in ~/.config/systemd/user/ (create the dir if does not exist), then you can

  systemctl --user daemon-reload
systemctl --user start <your service>.service
systemctl --user status <your service>.service

Also user can start "transient units" as themself, using the same user-bus, and then manage them (start, stop, kill, restart, etc) as regular units.

Transient units are exactly like regular units but cant survive server restarts...

you start them with systemd-run --user and then manage them with systemctl --user

example

$ systemd-run --user /bin/sleep 300
Running as unit: run-r6545f2e54ffc4f30b15f5dcabb280e5a.service

$ systemctl --user status run-r6545f2e54ffc4f30b15f5dcabb280e5a.service
● run-r6545f2e54ffc4f30b15f5dcabb280e5a.service - /bin/sleep 300
Loaded: loaded (/run/user/1000/systemd/transient/run-r6545f2e54ffc4f30b15f5
Transient: yes
Active: active (running) since Thu 2018-03-15 20:31:52 PDT; 15s ago
Main PID: 21095 (sleep)
CGroup: /user.slice/user-1000.slice/user@1000.service/run-r6545f2e54ffc4f30
└─21095 /bin/sleep 300

$ systemctl --user stop run-r6545f2e54ffc4f30b15f5dcabb280e5a.service

$ systemctl --user status run-r6545f2e54ffc4f30b15f5dcabb280e5a.service
Unit run-r6545f2e54ffc4f30b15f5dcabb280e5a.service could not be found.

Note on older systemd, make sure the dbus is running on your system, and accessible for the user, in general make sure you have DBUS_SESSION_BUS_ADDRESS and XDG_RUNTIME_DIR environment variable set.

How can I persist a sudo-session even when the calling user logs out?

Use nohup or make another tmux window.



Related Topics



Leave a reply



Submit