0

Here is the command I have which works

It's just a kill with an expression which returns a number

kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null

Here is the ssh I normally use

bash -c 'timeout  120s ssh -o StrictHostKeyChecking=no [email protected] "cd NightTest"'

I try to combine both of them

bash -c 'timeout  120s ssh -o StrictHostKeyChecking=no [email protected] "kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null"'

It doesn't work, my guess is that it gets mixed up with the ''.

Tried options

Escaping most of the ' in the kill commands :

 bash -c 'timeout  120s ssh -o StrictHostKeyChecking=no [email protected] "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'

Do not work either, I did many other tries but can't make it work.

Any ideas?

Added note

My system doesn't support pkill command

4
  • 3
    Why the bash -c '…' notation in the first place? Why not just timeout …? Using the single quotes for the bash -c '…' greatly complicates the processing. You need to get a level of single quotes around the command to be executed. Commented Jun 17, 2015 at 14:07
  • Your code when you combine both works for me. What do you mean by “it doesn't work”? Commented Jun 17, 2015 at 14:13
  • 1
    @Arkanosis: the combined version evaluates the $(ps …) on the local machine, not on the host you are ssh-connecting to. Commented Jun 17, 2015 at 14:14
  • thanks a lot! about the -c is because I normally send many command in a string ( command1 && command 2 ) Commented Jun 17, 2015 at 14:17

2 Answers 2

3

Drop the bash -c notation. Assuming you don't rewrite the command to use pkill, then you need something like:

timeout 120s ssh -o StrictHostKeyChecking=no [email protected] \
    'kill $(ps -ef | grep "[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector" | cut -f8 -d" ") &> /dev/null'

Note that I used double quotes inside the command to be executed. Fortunately, there was nothing in the command where it would matter whether single quotes or double quotes were used.

The version with 'embedded single quotes' doesn't work because you can't embed single quotes in a single-quoted string. You can, if you must, write '…'\''…' to get a single quote in between two ellipsis. The first ' terminates the current single quoted string (even if the preceding character is a backslash; there are no escapes in a single quoted string); the \' generates a single quote; the third ' in the centre group resumes the single quoted string again.

Thus, in your attempt:

bash -c 'timeout  120s ssh -o StrictHostKeyChecking=no [email protected] "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'

the Bash command sees:

timeout  120s ssh -o StrictHostKeyChecking=no [email protected] "kill $(ps -ef | grep \[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\ | cut -f8 -d\ ) &> /dev/null"

which means grep gets multiple arguments where you wanted one, etc. It completely breaks up the meaning.

Nesting quoting conventions is hard — really hard.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for the solution and plus giving an explanation about why what I did didn't work!!
3

It'd be easier if you use pkill to kill the desired process. It performs the searching and killing all in one go.

pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'

Then you can throw that into the SSH call:

bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no [email protected] pkill -f "matchbox-panel --titlebar --start-applets showdesktop,windowselector"'

What's the purpose of the bash -c, by the way? If you can get rid of that, then it's even simpler.

timeout 120s ssh -o StrictHostKeyChecking=no [email protected] pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'

2 Comments

thanks a lot!! the -c is so command are read from string (because normally I sent lots of command with &&)
actually in my system it doesn't work, it doesn't have the pkill so I really have to go with the kill command (limited bash)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.