Running a script as root but with different users inside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running a script as root but with different users inside
# 1  
Old 05-29-2014
Running a script as root but with different users inside

Hi All, my script.sh has the below lines, and i need to run the script as root or wam. please tell me if this will work

Code:
#!/bin/bash

sudo -t wam /usr/local/wam/stopwam -r     ------- this needs run as wam user
/usr/local/web/stopweb -a                             --- this needs to run as root
/sbin/init 6                                                   --- need to run as root.

i need to schedule this job, and i need to give user mandatorily (either root or wam) while scheduling it.

Kindly help
# 2  
Old 05-29-2014
You don't have to use sudo, you can use just su - <user> -c <command>

Code:
NAME
       su - run a shell with substitute user and group IDs

SYNOPSIS
       su [OPTION]... [-] [USER [ARG]...]

DESCRIPTION
       Change the effective user id and group id to that of USER.

       -, -l, --login
              make  the  shell  a  login  shell, clears all envvars except for
              TERM, initializes HOME, SHELL, USER, LOGNAME and PATH

       -c, --command=COMMAND
              pass a single COMMAND to the shell with -c

This User Gave Thanks to blackrageous For This Post:
# 3  
Old 05-29-2014
Hey thanks for replying, Please correct my understanding if i am wrong.

I have to schedule this job in autosys, for that i need to give with which user my script.sh should run.

1. As per your suggestion, I can schedule the job using root, and can give the commands inside script.sh as below
Code:
su -u wam -c /usr/local/wam/stopwam -r     ------- this needs run as wam user
/usr/local/web/stopweb -a                             --- this needs to run as root
/sbin/init 6                                                   --- need to run as root.

Kindly suggest.

Thanks in Advance !!
# 4  
Old 06-01-2014
Hey all, I used the below command but my script is unable to take " -r " as argument with the below command.
Code:
su -u wam -c /usr/local/wam/stopwam -r

But it is working for sudo -t wam /usr/local/wam/stopwam -r

Please suggest.

Last edited by Franklin52; 06-02-2014 at 03:34 AM.. Reason: Please use code tags
# 5  
Old 06-01-2014
Please use code tags as required by forum rules!

Try quoting the entire command:
Code:
su -u wam -c "/usr/local/wam/stopwam -r"

This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-02-2014
Thanks Rudi.. will check and get back to you. ....
# 7  
Old 06-02-2014
Is this for every reboot ? (you run init 6)

If so, then it might be simpler to break this up. When you run shutdown or init, the server will most likely (you don't give us the OS name or version) run a set of stop scripts and then a set of start script (although init 6 does a full shutdown just leaving a flag to cause a boot immediately afterwards)

What are you really trying to achieve? Let us know the OS name and version and we can suggest a neater way I'm sure.




Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parsing Root password inside a C shell script

Hello everyone, I want to check how long a remote computer is running (e.g. with the command uptime or who - b) The check should be done during login from none root user by a script which is called from .cshrc. My script works fine if I login as root but I want that everybody get the information... (5 Replies)
Discussion started by: Nadielosabra
5 Replies

2. Shell Programming and Scripting

Running .sh file inside a shell script

Hello, You might help a newbie like me, I am trying to run a .sh inside my shell script. After running that I need to execute below commands. Here's how my scripts looks like. Hope you can help: #!/bin/sh cd $ORACLE_HOME/owb/bin/unix ./OMBPlus.sh ---> goes to OMB+> directory cd... (10 Replies)
Discussion started by: aderamos12
10 Replies

3. Shell Programming and Scripting

help in running while loop inside a shell script

I have an input file at ./$1.txt with content of seq numbers like : 1234567890 1234589700 . . so on.. I need to take each seq nbr from the input file ,run the query below: select ackname,seqnbr from event where event_text like '%seqnbr( from the input file)' and redirect the... (11 Replies)
Discussion started by: rkrish
11 Replies

4. Shell Programming and Scripting

Shell script for to view all users & groups history in root

Dear All, I want to know all users & group history in one file, for root terminal through shell or any other option (5 Replies)
Discussion started by: kpoobathi
5 Replies

5. Shell Programming and Scripting

Issue running script as root

1) Environment:Red Hat Linux, bash shell Script to be run owned by user :myUser Home environment of myUser: pathto/home 2) ESP agent with root access will run JobXXX.sh su - myUser -c "/pathto/home/bin/script.sh" where script.sh has some echo statements and an exit statement in the end... (4 Replies)
Discussion started by: cj09
4 Replies

6. Shell Programming and Scripting

Running a script from if block inside another script

how do i run a script from if block inside another script? this is what i tried but it doesnt seem to work: if test $a -eq $w then sh /home/scripts/script1.bash fi (3 Replies)
Discussion started by: shishirkotkar
3 Replies

7. Shell Programming and Scripting

Running a unix script(which is calling another script inside that) in background

Hi all, I am having a script ScriptA which is calling a script ScriptB in the same server and copying files to second server and have to execute one script ScriptC in the second server. THis First script ScriptA is the main script and i have to execute this process continously. for Keeping... (2 Replies)
Discussion started by: rohithji
2 Replies

8. Cybersecurity

Running script through SSH as root

Hi all, I have a situation where I have a shell script that I need to run remotely on multiple *nix machines via SSH. Unfortunately, some of the commands in it require root access. I know that best practices for ssh entail configuring it so that the root account cannot log in, you need to... (4 Replies)
Discussion started by: irinotecan
4 Replies

9. UNIX for Dummies Questions & Answers

running sed inside script file

i am substituting some text in the xml file using sed, on shell directly it works fine, but when i run it inside script file, it say, the function cant be parsed, i think the prob is due to xml file, kindly help (4 Replies)
Discussion started by: bajaj111
4 Replies

10. Shell Programming and Scripting

Running a command or script as root

I'm writing an application (Progress language) that needs to: 1) load the contents of a cron table into the Progress application; 2) display this information in a human manner and allow a select group of people to update it (these people are logged in as themselves, not as root); 3) save... (3 Replies)
Discussion started by: rm-r
3 Replies
Login or Register to Ask a Question