Im always running a particular app as root because it requires more privileges. I usually just sudo it. I want to make an alias that is something similar to: alias myCmd='sudo nohup /path/to/binary > /dev/null 2>&1 &'

The problem arises when sudo prompts for password and then nothing will happen actually. How can it be done?

  • Why are you sending it to the background using & when you're already using nohup? – muru Jun 1 '16 at 11:54

Use a shell script instead of an alias.

in ~/.bashrc:

export PATH="$PATH":~/bin

your script in ~/bin/runthat:

sudo nohup /path/to/binary > /dev/null 2>&1 &

Then to run it, just do it like:

$ runthat

Or if you skipped the PATH step, then run it like:

$ ~/bin/runthat

If this is a system-wide (all users) script instead of just you, then put it in /usr/local/bin instead.

Or also you could use a function. eg. in ~/.bashrc:

runthat() {
    sudo nohup /path/to/binary > /dev/null 2>&1 &

Also just FYI and not a direct answer to the question, another alternative is to put it in the sudoers file:

sudo visudo
    (your editor pops up here)
    username ALL=NOPASSWD: /path/to/binary

And now it won't ask for a password at all. (which means lower security... maybe some security exploit will now let malware run this app too... so make sure you accept that small risk)

  • I was hoping to just an alias, without all the boilerplate, but I can work with that. Thanks – buddy123 Jun 1 '16 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.