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.

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.

JOB001.sh: This is run as root

 
#! /bin/bash
#
##    Job name =               JOB001
#
su - myUser -c "/apps/mtr/bin/GetStatus.sh"
exit $?

GetStatus.sh: This is run as myUser

 
#! /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

 
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 yet:p)

Thanks Perderabo for your help.

--edit---
The url is: Running script from other user rather than login user

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.

Got it working by modifying the JOB001.sh

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

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