Running multiple commands with su in Bash
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I would like to use su -c
to run as root multiple commands altogether. I don't want to use an extra script for this.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & " -c "ls -lh"
but it executes only the ls
not the first one.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
but it says that there is an error with the semicolon ;
.
Do you know how to do that?
bash su
migrated from stackoverflow.com Apr 4 '11 at 20:00
This question came from our site for professional and enthusiast programmers.
add a comment |
up vote
8
down vote
favorite
I would like to use su -c
to run as root multiple commands altogether. I don't want to use an extra script for this.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & " -c "ls -lh"
but it executes only the ls
not the first one.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
but it says that there is an error with the semicolon ;
.
Do you know how to do that?
bash su
migrated from stackoverflow.com Apr 4 '11 at 20:00
This question came from our site for professional and enthusiast programmers.
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I would like to use su -c
to run as root multiple commands altogether. I don't want to use an extra script for this.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & " -c "ls -lh"
but it executes only the ls
not the first one.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
but it says that there is an error with the semicolon ;
.
Do you know how to do that?
bash su
I would like to use su -c
to run as root multiple commands altogether. I don't want to use an extra script for this.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & " -c "ls -lh"
but it executes only the ls
not the first one.
I tried the following:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
but it says that there is an error with the semicolon ;
.
Do you know how to do that?
bash su
bash su
edited Feb 15 '17 at 19:46
codeforester
368314
368314
asked Apr 4 '11 at 14:14
Abruzzo Forte e Gentile
194136
194136
migrated from stackoverflow.com Apr 4 '11 at 20:00
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Apr 4 '11 at 20:00
This question came from our site for professional and enthusiast programmers.
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
12
down vote
accepted
This command runs fine:
su root -c "date; ls -lh"
But in this command:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
Since you have &
before ;
therefore you are getting errors. Try removing &
and re-executing the command.
Or you can run your command like this:
su root -c "(tcpdump -i wlan0 -s 1500 -w CCCCCC &); ls -lh"
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolons and it will work.
– G-Man
Feb 15 '17 at 20:04
add a comment |
up vote
2
down vote
As anubhava says, your
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
command is failing because you’re not allowed to have&
followed immediately by ;
.
You can see this just by typing the commands directly into the shell:
true & true
workstrue & ; true
doesn’t work
If you want the tcpdump
command to run in the background,
remove the ;
, as in
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ls -lh;"
(and you don't need the ;
at the end, either, but it doesn’t hurt).
add a comment |
up vote
1
down vote
I think the nohup
command will get you what you want too... tcpdump running in the background (no ampersand necessary):
su root -c "nohup tcpdump -i wlan0 -s 1500 -w CCCCCC ; ls -lh"
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to usenohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong —nohup
by itself does not cause a command to run in the background; you still have to type&
(which leaves us back where we started, with a syntax error in the command).
– G-Man
Feb 15 '17 at 20:11
add a comment |
up vote
0
down vote
Just to add to Anubhava's excellent answer:
You could use as well, like this:
su root -c " tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh"
is a little more efficient than
()
in that it doesn't create a sub shell. does need a space after
and a
;
before unless we have an
&
already.
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolon and it will work.
– G-Man
Feb 15 '17 at 20:05
add a comment |
up vote
-1
down vote
Use &&
to separate your commands like so:
$ su -c "ls && ls"
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run thetcpdump
process in the background,&&
does you no good whatsoever.
– G-Man
Feb 15 '17 at 20:14
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
This command runs fine:
su root -c "date; ls -lh"
But in this command:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
Since you have &
before ;
therefore you are getting errors. Try removing &
and re-executing the command.
Or you can run your command like this:
su root -c "(tcpdump -i wlan0 -s 1500 -w CCCCCC &); ls -lh"
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolons and it will work.
– G-Man
Feb 15 '17 at 20:04
add a comment |
up vote
12
down vote
accepted
This command runs fine:
su root -c "date; ls -lh"
But in this command:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
Since you have &
before ;
therefore you are getting errors. Try removing &
and re-executing the command.
Or you can run your command like this:
su root -c "(tcpdump -i wlan0 -s 1500 -w CCCCCC &); ls -lh"
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolons and it will work.
– G-Man
Feb 15 '17 at 20:04
add a comment |
up vote
12
down vote
accepted
up vote
12
down vote
accepted
This command runs fine:
su root -c "date; ls -lh"
But in this command:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
Since you have &
before ;
therefore you are getting errors. Try removing &
and re-executing the command.
Or you can run your command like this:
su root -c "(tcpdump -i wlan0 -s 1500 -w CCCCCC &); ls -lh"
This command runs fine:
su root -c "date; ls -lh"
But in this command:
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
Since you have &
before ;
therefore you are getting errors. Try removing &
and re-executing the command.
Or you can run your command like this:
su root -c "(tcpdump -i wlan0 -s 1500 -w CCCCCC &); ls -lh"
answered Apr 4 '11 at 14:20
anubhava
36135
36135
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolons and it will work.
– G-Man
Feb 15 '17 at 20:04
add a comment |
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolons and it will work.
– G-Man
Feb 15 '17 at 20:04
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a
;
between the tcpdump
and ls
commands — just leave out the semicolons and it will work.– G-Man
Feb 15 '17 at 20:04
Of course, there’s no reason to add parentheses unless the OP really really really wants to type a
;
between the tcpdump
and ls
commands — just leave out the semicolons and it will work.– G-Man
Feb 15 '17 at 20:04
add a comment |
up vote
2
down vote
As anubhava says, your
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
command is failing because you’re not allowed to have&
followed immediately by ;
.
You can see this just by typing the commands directly into the shell:
true & true
workstrue & ; true
doesn’t work
If you want the tcpdump
command to run in the background,
remove the ;
, as in
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ls -lh;"
(and you don't need the ;
at the end, either, but it doesn’t hurt).
add a comment |
up vote
2
down vote
As anubhava says, your
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
command is failing because you’re not allowed to have&
followed immediately by ;
.
You can see this just by typing the commands directly into the shell:
true & true
workstrue & ; true
doesn’t work
If you want the tcpdump
command to run in the background,
remove the ;
, as in
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ls -lh;"
(and you don't need the ;
at the end, either, but it doesn’t hurt).
add a comment |
up vote
2
down vote
up vote
2
down vote
As anubhava says, your
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
command is failing because you’re not allowed to have&
followed immediately by ;
.
You can see this just by typing the commands directly into the shell:
true & true
workstrue & ; true
doesn’t work
If you want the tcpdump
command to run in the background,
remove the ;
, as in
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ls -lh;"
(and you don't need the ;
at the end, either, but it doesn’t hurt).
As anubhava says, your
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh;"
command is failing because you’re not allowed to have&
followed immediately by ;
.
You can see this just by typing the commands directly into the shell:
true & true
workstrue & ; true
doesn’t work
If you want the tcpdump
command to run in the background,
remove the ;
, as in
su root -c "tcpdump -i wlan0 -s 1500 -w CCCCCC & ls -lh;"
(and you don't need the ;
at the end, either, but it doesn’t hurt).
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Feb 15 '17 at 19:57
G-Man
12.3k92961
12.3k92961
add a comment |
add a comment |
up vote
1
down vote
I think the nohup
command will get you what you want too... tcpdump running in the background (no ampersand necessary):
su root -c "nohup tcpdump -i wlan0 -s 1500 -w CCCCCC ; ls -lh"
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to usenohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong —nohup
by itself does not cause a command to run in the background; you still have to type&
(which leaves us back where we started, with a syntax error in the command).
– G-Man
Feb 15 '17 at 20:11
add a comment |
up vote
1
down vote
I think the nohup
command will get you what you want too... tcpdump running in the background (no ampersand necessary):
su root -c "nohup tcpdump -i wlan0 -s 1500 -w CCCCCC ; ls -lh"
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to usenohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong —nohup
by itself does not cause a command to run in the background; you still have to type&
(which leaves us back where we started, with a syntax error in the command).
– G-Man
Feb 15 '17 at 20:11
add a comment |
up vote
1
down vote
up vote
1
down vote
I think the nohup
command will get you what you want too... tcpdump running in the background (no ampersand necessary):
su root -c "nohup tcpdump -i wlan0 -s 1500 -w CCCCCC ; ls -lh"
I think the nohup
command will get you what you want too... tcpdump running in the background (no ampersand necessary):
su root -c "nohup tcpdump -i wlan0 -s 1500 -w CCCCCC ; ls -lh"
answered Apr 4 '11 at 14:38
mcmap
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to usenohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong —nohup
by itself does not cause a command to run in the background; you still have to type&
(which leaves us back where we started, with a syntax error in the command).
– G-Man
Feb 15 '17 at 20:11
add a comment |
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to usenohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong —nohup
by itself does not cause a command to run in the background; you still have to type&
(which leaves us back where we started, with a syntax error in the command).
– G-Man
Feb 15 '17 at 20:11
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
Hi I think so. NOHUP leaves commands running in background even when a user disconnect. I didn't remember that. Thanks a lot.
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:34
(1) It’s good to use
nohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong — nohup
by itself does not cause a command to run in the background; you still have to type &
(which leaves us back where we started, with a syntax error in the command).– G-Man
Feb 15 '17 at 20:11
(1) It’s good to use
nohup
when you run commands in the background, but that really has nothing to do with this question. (2) This answer is wrong — nohup
by itself does not cause a command to run in the background; you still have to type &
(which leaves us back where we started, with a syntax error in the command).– G-Man
Feb 15 '17 at 20:11
add a comment |
up vote
0
down vote
Just to add to Anubhava's excellent answer:
You could use as well, like this:
su root -c " tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh"
is a little more efficient than
()
in that it doesn't create a sub shell. does need a space after
and a
;
before unless we have an
&
already.
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolon and it will work.
– G-Man
Feb 15 '17 at 20:05
add a comment |
up vote
0
down vote
Just to add to Anubhava's excellent answer:
You could use as well, like this:
su root -c " tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh"
is a little more efficient than
()
in that it doesn't create a sub shell. does need a space after
and a
;
before unless we have an
&
already.
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolon and it will work.
– G-Man
Feb 15 '17 at 20:05
add a comment |
up vote
0
down vote
up vote
0
down vote
Just to add to Anubhava's excellent answer:
You could use as well, like this:
su root -c " tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh"
is a little more efficient than
()
in that it doesn't create a sub shell. does need a space after
and a
;
before unless we have an
&
already.
Just to add to Anubhava's excellent answer:
You could use as well, like this:
su root -c " tcpdump -i wlan0 -s 1500 -w CCCCCC & ; ls -lh"
is a little more efficient than
()
in that it doesn't create a sub shell. does need a space after
and a
;
before unless we have an
&
already.
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Feb 15 '17 at 19:34
codeforester
368314
368314
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolon and it will work.
– G-Man
Feb 15 '17 at 20:05
add a comment |
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a;
between thetcpdump
andls
commands — just leave out the semicolon and it will work.
– G-Man
Feb 15 '17 at 20:05
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a
;
between the tcpdump
and ls
commands — just leave out the semicolon and it will work.– G-Man
Feb 15 '17 at 20:05
Of course, there’s no reason to add braces or parentheses at all unless the user really really really wants to type a
;
between the tcpdump
and ls
commands — just leave out the semicolon and it will work.– G-Man
Feb 15 '17 at 20:05
add a comment |
up vote
-1
down vote
Use &&
to separate your commands like so:
$ su -c "ls && ls"
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run thetcpdump
process in the background,&&
does you no good whatsoever.
– G-Man
Feb 15 '17 at 20:14
add a comment |
up vote
-1
down vote
Use &&
to separate your commands like so:
$ su -c "ls && ls"
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run thetcpdump
process in the background,&&
does you no good whatsoever.
– G-Man
Feb 15 '17 at 20:14
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Use &&
to separate your commands like so:
$ su -c "ls && ls"
Use &&
to separate your commands like so:
$ su -c "ls && ls"
answered Apr 4 '11 at 14:20
nickgrim
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run thetcpdump
process in the background,&&
does you no good whatsoever.
– G-Man
Feb 15 '17 at 20:14
add a comment |
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run thetcpdump
process in the background,&&
does you no good whatsoever.
– G-Man
Feb 15 '17 at 20:14
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
Auch..that's my error. I wanted to run the process in background but I was not aware of this && to separate commands. Thx
– Abruzzo Forte e Gentile
Apr 4 '11 at 15:35
2
2
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
&& means "Only execute the second command if the first succeeded". If your first command errors out, the second one won't run. This is usually good, but I would hesitate to describe && as purely a separator.
– Leons
Feb 21 '13 at 15:50
And, if you want to run the
tcpdump
process in the background, &&
does you no good whatsoever.– G-Man
Feb 15 '17 at 20:14
And, if you want to run the
tcpdump
process in the background, &&
does you no good whatsoever.– G-Man
Feb 15 '17 at 20:14
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f10601%2frunning-multiple-commands-with-su-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown