Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Apr 4 '11 at 20:00

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

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"
share|improve this answer
add comment

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"
share|improve this answer
    
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
add comment

Use && to separate your commands like so:

$ su -c "ls && ls"
share|improve this answer
    
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
1  
&& 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.