13

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?

5 Answers 5

17

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"
1
  • 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. Feb 15, 2017 at 20:04
4

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  works
  • true & ; 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).

1

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"
2
  • 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, 2011 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). Feb 15, 2017 at 20:11
0

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.

1
  • 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. Feb 15, 2017 at 20:05
-1

Use && to separate your commands like so:

$ su -c "ls && ls"
3
  • 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, 2011 at 15:35
  • 3
    && 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.
    – Leo
    Feb 21, 2013 at 15:50
  • And, if you want to run the tcpdump process in the background, && does you no good whatsoever. Feb 15, 2017 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .