0

I want to run a command in the background as root. And I have a problem with doas command.

With sudo, I can use sudo --background <command>. It will ask for password, then runs in the background. I cannot do doas <command> & because it will ask for the password in the background and stuck there forever.

I've checked the manpage but did not find the equivalent option for doas.

2
  • 1
    Does doas have the equivalent of sudo -v (basically just prompt for authentication to cache credentials for a while)? Then you could authenticate once, then run doas <command> & and it would use the cached credentials. If it doesn't, you can still try something like doas true; doas <command> &
    – muru
    Commented Apr 22, 2023 at 9:36
  • @muru It is a nice trick.
    – Livy
    Commented Apr 23, 2023 at 1:42

1 Answer 1

1

One way of doing this would be to start an in-line script with either sh -c or bash -c (or whatever shell your command needs) using doas, and let that script run the command in the background. To run multiple sequential commands in the background, collect them in a subshell:

doas sh -c '(sleep 10; echo done) &'

The above command would start a child shell running as root, with a backgrounded subshell that waits 10 seconds before printing the string done. The control of the initial non-root interactive shell would be handed back to the user as soon as the command in the in-line script has started in the background.

This would also work with sudo.

3
  • I've noticed that jobs command shows the command running in the background if I use sudo --background, but not when using doas sh -c. Maybe because sudo --background does not spawn a subshell? Is there anything I can do to control the status of the subshell process spawned by doas sh -c?
    – Livy
    Commented Apr 23, 2023 at 1:37
  • Keeping track of the subshell pid is a pain to deal with compared to a simple 'jobs' command.
    – Livy
    Commented Apr 23, 2023 at 3:15
  • @Livy Why would you need to keep track of the PID of the background process? It's not a process that belongs to your user so you can't really do anything with it. It would be simple enough to print the PID from the inline script, but your question never said anything about these requirements.
    – Kusalananda
    Commented Apr 23, 2023 at 5:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .