Linux, exit command in a script

I am using RHEL 6.x

I asked a similar question recently, but apparently did not understand

Originally I was in root, and I performed a su - username

From the user, I want to exit to root and run a root command

Something like

echo 1
echo 2
service network stop
echo 3

Open in new window

Obviously you never get to echo 2, because the exit will exit from the script

How can I resolve this issue ?

Los Angeles1Asked:
Who is Participating?
omarfaridConnect With a Mentor Commented:
Can you elaborate more?

To run commands as other users, you can do the following in the script

su username -c command

run the script as root user, the first one with su will run as username and the othercommand will run as root
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
You can put the commands you need  to run as username in a script, call that script the way omarfarid is suggesting. Once the username's shell exits, you cannot run commands as root anymore. At least not the way you are describing. Maybe it would help if you would post some real code (changing details if needed) so we understand and can help you better.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.