How to Sudo the Current Process

How do I sudo the current process?

Unfortunately, I'm not aware of a way to do what you want to do cleanly. I think your best bet is to make the program setuid (or run it under sudo) and then either do your dirty work and drop permissions, or fork() and drop permissions from one process and keep the other one around to do your root work.

What you're looking for are the setuid(2) / setreuid(2) / setregid(2) / setgroups(2) calls, but they are all hard wired to not allow you to gain privileges mid-invocation. You can only use them to "give away" privileges, as far as I know.

How to get the pid of command running with sudo

for this purpose I will enter

sudo gvim &

ps aux | grep gvim

supplies me with the following output

root 11803 0.0 0.0 12064 2776 pts/3 T 12:17 0:00 sudo gvim

to grab only the pID i prefer to use awk

ps aux | awk '/gvim/ {print $2}'

which would return simply

11803

I could kill the program from awk as well by piping a kill command to bash

ps aux | awk '/gvim/ {print "sudo kill -9 "$2}' | bash

Getting sudo and nohup to work together

The problem here, imho, is not nohup, but background processing sudo.

You are putting the process in background (& at end of command) but probably sudo needs password authentication, and that is why the process stops.

Try one of these:

1) remove the ampersand from end of command, reply to passord prompt and afterwords put it in background (by typing CTRL-Z - which stops the process and issuing the bg command to send it to background)

2) Change the /etc/sudoers to not ask for users password by including the line:
myusername ALL=(ALL) NOPASSWD: ALL

If besides the password reply your application waits for other input, then you can pipe the input to the command like this:
$ cat responses.txt|sudo mycommand.php

hth

Run sudo -s inside shell script

Every command in a shell script, is run independently. The script is parent process which invokes commands as child processes. Thus, sudo -s creates a new process that opens root shell. However, this process cannot execute the commands later.

You can also see your echo output being printed, if you do exit from root shell. This happens because, when you exit, the process of root shell gets terminated.

You can write all the commands, except sudo -s in shell script. Make it executable by chmod +x install_logentries.sh. And execute it via sudo install_logentries.sh

Another alternative is to embed commands as a child process using << (as given below):

#!/bin/bash
sudo -s << SCRIPT
tee /etc/yum.repos.d/logentries.repo <<EOF

[logentries]
name=Logentries repo
enabled=1
metadata_expire=1d
baseurl=http://rep.logentries.com/amazonlatest/\$basearch
gpgkey=http://rep.logentries.com/RPM-GPG-KEY-logentries

EOF
yum update
yum install logentries
SCRIPT

Running a program in the background as sudo

The problem is that the sudo command itself is being run in the background. As a result, it will be stopped (SIGSTOP) when it tries to access the standard input to read the password.

A simple solution is to create a shell script to run synaptic & and then sudo the script in the foreground (i.e. without &).

how to terminate a process which is run with sudo? Ctrl+C do it, but not kill

Try the -Z option to tcpdump. It instructs tcpdump to drop root privileges and run as the user specified in the argument.

sudo tcpdump -Z $USER -ieth1 -w ~/dump.bin

Now try killing that process.



Related Topics



Leave a reply



Submit