Issue running script as root


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue running script as root
# 1  
Old 05-24-2011
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


Current behaviour when running the script:
root@ServerName1>./JobXXX.sh

(and then the control remains on the following path)
myUser@Servername1[pathto/home]
(have to print "exit" to get the output of echo statements and ctrl back to root@ServerName1


Expected behaviour when running the script:
root@ServerName1>./JobXXX.sh
  • Runs /pathto/home/bin/script.sh
  • Prints the echo statements
  • ctrl is root@ServerName1

Any suggestions how to fix the issue ?

Thanks for help.
# 2  
Old 05-24-2011
If the script really is "just a few echo statements and then an exit", then it's missing the leading shebang line and which shell runs it is up for grabs. Try putting in a shebang like #! /bin/bash or whatever is appropriate to your environment. Also, if it's that short, post it so we can see it.

I tend to think that the problem is elsewhere though. Try su - myUser -c "/bin/hostname" changing the path to where your hostname command resides. If that fails the script is exonerated and the problem probably lies with the login scripts for myUser.
# 3  
Old 05-24-2011
JOB001.sh: This is run as root
Code:
 
#! /bin/bash
#
##    Job name =               JOB001
#
su - myUser -c "/apps/mtr/bin/GetStatus.sh"
exit $?

GetStatus.sh: This is run as myUser
Code:
 
#! /bin/bash
echo "Printing the Status"
#have to add some code but for debugging purpose presently just have echo statement
exit 0

---------- Post updated at 09:32 PM ---------- Previous update was at 09:09 PM ----------

Finally got it working by modifying the JOB001.sh

Code:
 
su - myUser<<! -c "/apps/mtr/bin/GetStatus.sh"

(am not sure what myUser<<! does but it works)

This was after searching the forum and found a reply(from user pchangba1) which worked.
(couldn't include the url because it requires to have atleast 5 posts to this site before one can post a url which i don't have as yetSmilie)

Thanks Perderabo for your help.

--edit---
The url is: https://www.unix.com/shell-programmin...ogin-user.html

Last edited by Perderabo; 05-25-2011 at 08:06 AM.. Reason: add url
# 4  
Old 05-24-2011
Now I really suspect the login scripts. I would pepper them with statements like echo got to point 1 and see how far they run.
# 5  
Old 05-24-2011
Got it working by modifying the JOB001.sh

Code:
 
su - myUser<<! -c "/apps/mtr/bin/GetStatus.sh"

(am not sure what myUser<<! does but it works)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with running a script via ssh

Hi, I'm trying to run a user defined shell script with options and arguments via ssh but getting error as ksh: Script.sh: not found. Here is what i'm running: ssh -t username@server 'cd /path/to/script; script.sh -t start here '-t' with script.sh, is an user defined option and 'start' is also... (3 Replies)
Discussion started by: xsam
3 Replies

2. Programming

Running a script as root in the script

hi all, i think i have got the solution for this but want to run it past you guys first when i run a script sometimes its necassary to sudo to root so it can create users, chmod etc etc, the normal way for me doing this is just to simply run the script as root but i have created a user... (11 Replies)
Discussion started by: robertkwild
11 Replies

3. Shell Programming and Scripting

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 #!/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... (18 Replies)
Discussion started by: nanz143
18 Replies

4. Shell Programming and Scripting

Issue with running commands from shell script

I'm trying to copy files from a remote windows server to Unix server. I was successfully able to copy files from windows server using command prompt but when I run these commands from a script it's not working as expected. commands used: sftp user@remoteserver.com lcd local_dir cd... (3 Replies)
Discussion started by: naresh7590
3 Replies

5. Shell Programming and Scripting

Issue in running shell script in crontab

I'm having a shell script which has to be run only once at the specified time. Shell script is like following, #!/bin/bash db2 connect to XXX > connection_status.txt This script is scheduled in crontab as following, 50 4 8 5 0 sh script.sh scheduled script is run at the specified time... (1 Reply)
Discussion started by: Rajkumar Gandhi
1 Replies

6. Shell Programming and Scripting

Script for running root based C++ code

Hi all, I have to run C++ file using root programming, using following commands: $root -l root .L TwoTrees.C++ root TwoTrees t root t.Loop() root.q I wonder if I can write script to do the following. Thanks Pooja (12 Replies)
Discussion started by: nrjrasaxena
12 Replies

7. Shell Programming and Scripting

issue with running script with crontab

I am facing a strange issue while running a script(eg A) from the crontab entry the script calls one more script(eg B) within it now when i run the script A manually(with nohup) it also executes the script B (embedded inside it) as expected. but when i run the script A from the crontab entry... (7 Replies)
Discussion started by: mad_man12
7 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. Shell Programming and Scripting

As root , running script as different user with su - problem

Dear All I am running into a situation where I am running a script as another user lets say oracle using su command as below, and the script fails because the .profile of oracle is not executed so the environment variables are not set. cat /etc/passwd | grep oracle... (4 Replies)
Discussion started by: dbsupp
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