1

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?

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

2 Answers 2

3

Use a shell script instead of an alias.

in ~/.bashrc:

export PATH="$PATH":~/bin

your script in ~/bin/runthat:

#!/bin/bash
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)

2
  • I was hoping to just an alias, without all the boilerplate, but I can work with that. Thanks
    – buddy123
    Jun 1, 2016 at 15:21
  • I was beating myself over the head trying to get these aliases to work. I knew I could create bash scripts but thought it would be better to use aliases. My perfectionist nature wouldn't let me give it up. Anyone reading this..just use scripts. Don't be like me lol
    – waltmagic
    Dec 29, 2022 at 4:10
0

Add this at the bottom of your ~/.bashrc

alias sudo="sudo " # space after sudo

You must log in to answer this question.

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