Search code examples
linuxbashubuntusublimetextbuild-system

Sublime Text build system on Ubuntu: Cannot chain pkill command regardless of success


I'm trying to first kill a running process

pkill -f "java.*MySketch"

then run it again

processing-java --sketch="~/MySketch" --output="~/MySketch/build-tmp" --run --force

However on first build the process isn't running so pkill finds nothing. I want the build system to skip this error and move on to executing processing-java.

Processing.sublime-build

  "linux": {
    // Close old sketch on build
    "shell_cmd": "pkill -f \"java.*$file_base_name\" ; processing-java --sketch=\"$file_path\" --output=$file_path/build-tmp --run --force"
  },

I've tried using ; and appending || true at the end of the pkill command. Also tried pgrep -f "java.*MySketch" | xargs kill > /dev/null 2>&1 but it's still not working in Sublime Text. Moving everything to a build.sh script also doesn't work. It works in the terminal. In Sublime it says Finished in 0.3s with exit code -15. So I guess instead of failing to find a process and doing nothing, the termination signal (SIGTERM) ends my whole shell_cmd?! SIGKILL (pkill -9 -f ...) too.

Only thing I've managed to get working in Sublime Text is a blanket case killing all java processes, pkill java ; processing-java bla bla bla

Thanks!

UPDATE: pkill is killing itself first because my regex pattern is not smart enough to match the real java process only.


Solution

  • The problem was this build system was killing itself first, because its own commandline matched java.*MySketch.

    Solution: Replaced \"java.*$file_base_name\" with \"java.*$file_path\\$\"

    Details: Looked at the commandline of the process I want to kill and saw that it ended with --sketch-path=~/Sketchbook/MySketch, whereas the build system's commandline ends with --force. Used (blabla)$ regex for matching the end of the string. Because of the Sublime Build system, had to escape it with extreme prejudice... \\$.