Execute multiple commands in one go.

narenv

New Member
Joined
Apr 21, 2020
Messages
1
Reaction score
0
Credits
0
hi all,

I need to 1.switch to a particular user. 2. go to particular folder. 3. check status of an application. in just one go, for which I have added all the commands in the following format in a file and then executed that file like this ./file but I can see only the first 1.switch to particular user was successfull and didnt see other 2 commands getting executed.

contents of my file :

sudo su - jack \
&& cd \opt\jae \
&& ./appctl status

I also tried

sudo su - jack && cd \opt\jae\ && ./appctl status ..

Both executes only the first command and stops. Can someone please help.
 


Try this:
Code:
sudo -i -u jack bash << EOF
    cd /opt/jae && ./appctl status
EOF

Do you need to be in /opt/jae to run appctl?
If not, you could change it to:
Code:
sudo -i -u jack bash << EOF
    /opt/jae/appctl status
EOF

Whatever is more appropriate, the above uses sudo to switch to user jack. It runs bash and uses a heredoc to specify the commands to run as the user. After the sudo command has completed, you will be switched back to your account.

This was based on something I found here:
https://stackoverflow.com/a/24696790

See also:
https://superuser.com/a/468163
 

Members online


Top