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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.