Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute multiple commands after sudo command

Tags:

linux

shell

unix

I need to run a batch to login to server, get in sudo su - username and run specific commands.

I tried below code:

putty username@servername -pw password -m myshell.sh

myshell.sh:

#!/bin/sh
sudo su - username
cd to particular folder
then tail a file

i am getting in sudo, but after that the script stucks until i logout.

like image 982
Suraj Modi Avatar asked Mar 17 '23 17:03

Suraj Modi


2 Answers

You can use sh -c and then use semicolons between commands, I'd consider the solution suggested in the comments though, just have whole script run as sudo.

sudo sh -c "cd /tmp;pwd;cd /dev;pwd""
like image 124
Adam Avatar answered Mar 25 '23 06:03

Adam


updated my shell file with below command and it worked:

#!/bin/sh
sudo su - username << block
cd /; 
tail filename;
block

all the commands are to be written in block and separated by ";"

like image 31
Suraj Modi Avatar answered Mar 25 '23 06:03

Suraj Modi