top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

shell script where first statement is sudo su and rest of the script is executed as root..

+3 votes
306 views

When I tried to run this kind of script unless I exit rest of the script is not getting executed.

posted Aug 20, 2013 by Ovinod Nair

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Try the following way

#!/bin/sh
[ `whoami` = root ] || exec su -c $0 root
//your rest of the script 

exec, which means "replace this program by", which effectively ends its execution and starts the new program, launched by su, with root, to run from the top

answer Aug 20, 2013 by Deepankar Dubey
Similar Questions
0 votes

I'm trying to write a python script that writes some content to a file root through sudo, but it's not working at all. I am using:

 channel = 'stable'
 config_file = '/opt/ldg/etc/channel.conf'
 command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]
 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 out, _ = p.communicate()

But it seems as if this isn't doing anything.

I just want to write the contents of the variable channel to the file /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can anyone offer any pointers?

0 votes
+2 votes

I have a process in Linux which can have different memory footprint, I want a program or shell script which can print the time of the day when the memory usage is highest for this process.

Can someone help me.

+3 votes
+1 vote

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

...