1834571 Members
3295 Online
110069 Solutions
New Discussion

Re: $? from su

 
Sylvain CROUET
Respected Contributor

$? from su

Hello!

I use a script to start several applications. Each application is started with a different user, using the su - command.
I would like to get within the main script the exit status of the command used within the su to start the application.
How can I do it?
7 REPLIES 7
Tim Adamson_1
Honored Contributor

Re: $? from su

Try the following:

su -c ";[[ $? -eq 0 ]] && echo ok"

I'm nowhere near a server so I might have the syntax a bit screwed, but the concept should work. :-)


Tim


Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
Stefan Farrelly
Honored Contributor

Re: $? from su

Probably the easiest thing to do is each time you call su write a message with the return status to a logfile in /tmp, eg;

su - -c ";echo started app return status = $? >>/tmp/log"

Then check the log afterwards.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Stefan Farrelly
Honored Contributor

Re: $? from su

On second thought how about;

su - -c ";[[ $? -ne 0 ]] && exit $?"
[ $? -ne 0 ] && echo bad return status from trying to start application

This will return any bad return status from the start app command back to the su command to so you can check for the value of $? immediately after the su.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Bill Hassell
Honored Contributor

Re: $? from su

Well, this may be tricky. If su - fails (like a bad password) then you'll get a non-zero exit code from su - (like 9 for bad password, 13 for badlogin)). However, if su - is successful, it will start the user's shell and run whatever you have in your script. But then the return code may contain the exit code of the task rather than su. SO you may need to code a special flag (a zero length file for instance) to signal your parent script that all is well with su.


Bill Hassell, sysadmin

Re: $? from su

I thought su returned the exit code of whatever it ran anyway? As in the following examples:

# su oracle -c true
# echo $?
0
# su oracle -c false
# echo $?
1
# su oracle -c "exit 3"
# echo $?
3


HTH

Duncan

I am an HPE Employee
Accept or Kudo
Sylvain CROUET
Respected Contributor

Re: $? from su

Thanks!

In fact the only solution I already found was the temporary file containing the exit status, solution proposed by Stefan. I hoped there was a better solution.
Seth Parker
Trusted Contributor

Re: $? from su

I'm facing a similar problem so I tried Duncan's solution. However, I noticed that the return value was always 0 for one of the users. Turns out this user has a different shell (PEEK shell) than the others. So, for /sbin/sh and /usr/bin/sh I was able to get "su - -c 'exit 3'" to return 3.

Bill, though I understand what's happening in the case of the PEEK shell, why would su sometimes return the task exit code instead of the command code?

Wouldn't Tim's or Stefan's solution handle that by making the task's exit code be the command's exit code?

Just curious (sorry for the follow-on!).

Regards,

Seth