1826421 Members
3503 Online
109692 Solutions
New Discussion

Re: nohup an su command

 
SOLVED
Go to solution
Jay Core
Frequent Advisor

nohup an su command

Alright everyone - easy pointage opportunity!

I'm trying to nohup, su, and then run a command and write it to a file - but no luck. Here's what I'm doing:

/usr/bin/nohup "/usr/bin/su oracle -c uptime" 2>&1 > /tmp/oratmp.txt &

I want the results of uptime to be written to /tmp/oratmp.txt - but it doesn't even get created.
What is my major malfunction?

Thanks all!
JC
15 REPLIES 15
Bill Hassell
Honored Contributor

Re: nohup an su command

One problem is with the order of your redirection statements. 2>&1 says redirect the output of stderr to your stdout device, in this the terminal. The next: > /tmp... says change stdout to go the file. But stderr is not going to the file. The correct order is to redirect file descriptor 1 before you refer to that file descriptor, like this:

> /tmp/oratmp.txt 2>&1

Now the file descriptor 1 will be assigned to the filename and descriptor 2 will use what descriptor 1 is using.

/usr/bin/nohup and /usr/bin/su are OK ways to ensure your PATH doesn't pickup a rogue copy of the desired program but you have to be consistent (ie, /usr/bin/uptime). But a much better way to handle $PATH is to set it on the command line:

PATH=/usr/bin nohup su oracle -c uptime > /tmp/oratmp.txt 2>&1 &

The assignment of PATH on the command line is temporary so it doesn't affect your current $PATH value. For scripts, I always start with:

export PATH=/usr/bin

and then add additional paths as required. That way, my standalone scripts never inherit an unknown $PATH and run the wrong command.


Bill Hassell, sysadmin
Aashique
Honored Contributor

Re: nohup an su command

Hi,
use this:
/usr/bin/nohup "/usr/bin/su oracle -c uptime">> /tmp/oratmp.txt &

Thanks & Regards

Aashique
Jay Core
Frequent Advisor

Re: nohup an su command

Thanks Bill and Aashique,

I really appreciate the feedback, but I'm still coming up with an empty file.

Things that make you go HMMM?
Dennis Handly
Acclaimed Contributor

Re: nohup an su command

>Aashique: use this:
/usr/bin/nohup "/usr/bin/su oracle -c uptime">> /tmp/oratmp.txt &

This doesn't look right but may work? The syntax for nohup is:
nohup command arguments
If you quote it, it is all one "command".
You would have to use tusc to see what's happening.

>but I'm still coming up with an empty file.

You may need to use tusc to see what's happening.

You can also break up the command into a script:
nohup script > /tmp/oratmp.txt 2>&1 &

And in the script use:
#!/usr/bin/sh
su oracle -c "uptime"

With a script you can add echo and "set -x" to debug.
Jay Core
Frequent Advisor

Re: nohup an su command

Thanks Dennis. The problem is that I cannot invoke a script in the command, and I cannot use tusc - thanks asnyway.
Dennis Handly
Acclaimed Contributor

Re: nohup an su command

>The problem is that I cannot invoke a script in the command, and I cannot use tusc.

You use these only for debugging.

I had no problems doing this from root:
# nohup su xxxxxx -c "uptime" > /tmp/oratmp.txt 2>&1 &

So your original problem had to do with quoting the nohup options and your incorrect order of redirections.
Jay Core
Frequent Advisor

Re: nohup an su command

Thanks Dennis - still no output - shucks!
Dennis Handly
Acclaimed Contributor

Re: nohup an su command

>still no output

I'm not sure why?

What do you get for:
/usr/bin/nohup /usr/bin/su oracle -c "/usr/bin/uptime"
Jay Core
Frequent Advisor

Re: nohup an su command

Hey Dennis,

still nothing. Sorry, and thanks for your efforts.

JC
Dennis Handly
Acclaimed Contributor

Re: nohup an su command

>still nothing.

I assume you are root and your shell is /sbin/sh and the user oracle exists?
Bill Hassell
Honored Contributor
Solution

Re: nohup an su command

> still nothing.

How about just the basics? Start with:

/usr/bin/su - oracle
uptime

Notice the "-" with su? Unless you are very familiar with using su without the "-", always use su - so your session inherits the correct environment.

If the above works OK, now try it with PARTIAL redirection:

/usr/bin/su - oracle -c uptime > /tmp/oratmp.txt

The reason for partial is that you need to see the errors in case /tmp/oratmp.txt is causing a problem. (perhaps there is an existing file called oratmp.txt but owned by root and allowing no write access) Because stderr is redirected to stdout, failure to create the temp file will be invisible because the error message cannot be written out.

Now the same with complete redirection:

/usr/bin/su - oracle -c uptime > /tmp/oratmp.txt 2>&1

This should produce the desired output. Now remove the output file so you can see if it gets created again in the background:

rm /tmp/oratmp.txt
/usr/bin/su - oracle -c uptime > /tmp/oratmp.txt 2>&1 &

And if that works OK, try it with nohup as a last step:

rm /tmp/oratmp.txt
/usr/bin/nohup /usr/bin/su - oracle -c uptime > /tmp/oratmp.txt 2>&1 &

Another way to troubleshoot this would be to just remove the stderr redirection:

rm /tmp/oratmp.txt
/usr/bin/nohup /usr/bin/su - oracle -c uptime > /tmp/oratmp.txt &

Then look in the local file nohup.out. It will contain any stderr problems.


Bill Hassell, sysadmin
Jay Core
Frequent Advisor

Re: nohup an su command

Dennis - yes to both - thanks.

Bill - wow - thanks for the insight - I'll check it out and let you know - Thanks!
Jay Core
Frequent Advisor

Re: nohup an su command

Hi Bill - still having weirdo problems:


This worked as expected
____________________________________________

/tmp/oratmp.txt

This worked as expected, writing the "su - oracle" output and the "uptime" output to the file, although there were some control characters in there between the two command outputs, shown here: ^[[3g ^[H ^[H ^[H ^[H ^[H
^[H ^[H ^[H ^[HCOLUMNS=80
____________________________________________


&1

This worked as expected also, writing the "su - oracle" output and the "uptime" output to the file, although there were some control characters in there between the two command outputs as above, and also the statement: "not a terminal"
___________________________________________

&1 &


This appeared to hang, until I hit the return key twice, and it gave it a sigterm kill. There was an empty output file generated.
____________________________________________
/tmp/oratmp.txt 2>&1 &

This also appeared to hang, until I hit the return key twice, and it gave it a sigterm kill. There was an empty output file generated.
____________________________________________

/tmp/oratmp.txt &

This also appeared to hang, until I hit the return key twice, and it gave it a sigterm kill. There was an empty output file generated, and there wasn't any nohup.out file created.
____________________________________________

Sorry to keep bugging - any other ideas?

Thanks,
JC


Dennis Handly
Acclaimed Contributor

Re: nohup an su command

>Sorry to keep bugging - any other ideas?

It's all obvious now. What shell are you using? If you are using the scummy C shell, I've had that hang happen!

You need to go through every stinkin' line of /etc/profile, ~/.profile, .login, .cshrc etc to make sure all tty commands are conditioned out if you aren't on a tty.

These shouldn't be there:
>some control characters in there between the two command outputs, shown here: ^[[3g ^[H ^[H ^[H ^[H ^[H^[H ^[H ^[H ^[HCOLUMNS=80
>also the statement: "not a terminal"
>This appeared to hang,

See Bill's comments about evil tty stuff in these links:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1155618
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1206345
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1146959
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1132303
Bill Hassell
Honored Contributor

Re: nohup an su command

> This worked as expected, writing the "su - oracle" output and the "uptime" output to the file, although there were some control characters in there between the two command outputs, shown here: ^[[3g ^[H ^[H ^[H ^[H ^[H
^[H ^[H ^[H ^[HCOLUMNS=80

Your oracle login is messed up bad with hardcoded TERM values and interactive terminal commands. You have to fix the oracle .profile to bypass terminal commands when running in batch -- as mentioned before.


Bill Hassell, sysadmin