pkill returns 255 in combination with another command via remote ssh
The documentation for the pkill -f
option says:
-f
The pattern is normally only matched against the process name. When -f is set, the full command line is used.
So pkill -f xyz
will kill any process with "xyz" anywhere on its command line.
When you run ssh <remoteHost> 'source /etc/profile; pkill -f xyz'
, the remote ssh server will run the equivalent of this on your behalf:
$SHELL -c 'source /etc/profile; pkill -f xyz'
The resulting shell instance is a process with "xyz" in its command line. My guess is that pkill
is killing it, and ssh is reporting the killed session as exit code 255, like this:
$ ssh localhost 'kill $$'
$ echo $?
255
It doesn't happen when you just run ssh <remoteHost> 'pkill -f xyz'
, because some shells like bash will optimize for this case. Instead of running pkill as a subprocess, the shell instance will replace itself with the pkill process. So by the time pkill runs, the shell process with "xyz" on its command line is gone.
You can probably work around this by running pkill like this:
ssh <remoteHost> 'source /etc/profile; exec pkill -f xyz'
If that doesn't work, you can specify the pkill pattern in such a way that it doesn't match the pattern itself. For example:
ssh <remoteHost> 'source /etc/profile; exec pkill -f "[x]yz"'
The pattern [x]yz
matches the text "xyz", so pkill will kill processes where the text "xyz" appears. But the pattern doesn't match itself, so pkill won't kill processes where the pattern appears.
Jenkins, Host key verification failed, script returned exit code 255
It seems that the solution was to add the parameter StrictHostKeyChecking to the shell script line
sh "ssh -o StrictHostKeyChecking=no user@product.company.com 'echo $HOME'"
Exit codes 127 and 255 in Robot Framework
To execute commands that are not built-in. Directly write them to the terminal using "Write" keyword.
Use Write keyword in SSHLibrary. It pastes the command on the terminal and the command gets executed.
Related Topics
How to Create a File in Linux from Terminal Window
Attach to a Processes Output for Viewing
Fastest Way to Tell If Two Files Have the Same Contents in Unix/Linux
Ssh: Could Not Resolve Hostname [Hostname]: Nodename Nor Servname Provided, or Not Known
Cmd 2>&1 > Log VS Cmd > Log 2>&1
What Does "Bash:No Job Control in This Shell" Mean
Shellscript to Monitor a Log File If Keyword Triggers Then Execute a Command
Error: Gdal-Config Not Found While Installing R Dependent Packages Whereas Gdal Is Installed
Git Enter Long Passphrase for Every Push
Linux Shell Script for Each File in a Directory Grab the Filename and Execute a Program
Use Grep to Find Content in Files and Move Them If They Match
Using Rsync Include and Exclude Options to Include Directory and File by Pattern
Read Line by Line in Bash Script
How to Find Files Excluding Symbolic Links
Run Bash Script from Another Script Without Waiting for Script to Finish Executing