Scala check if external process id (pid) is currently running?

java process get pid
java process example
java process destroy not working
java process waitfor timeout example
java kill process by pid
if a process is running in background mode then first you would need to get its job id using
processbuilder waitfor
types of process in linux

I need to check if a specified process is currently running using Scala.

All I have is the PID.

Does Scala have an internal function or do I need to parse it using ps?

Thank you.

AFAIK, Java or Scala doesn't have such functionality. If you are on UNIX based machine, yes, your best bet is to use ps command.

You can use the PID with ps command as follows:

ps -p 8238 -o "pid="

Here PID is 8283, and we ask ps to search for it, and if it exists, just print it.

scala> import sys.process._
import sys.process._

scala> def processExists(pid: Int) = pid == {try { (List("ps", "-p", s"$pid", "-o", "pid=") !!).trim.toInt } catch { case _: Throwable => -1 }}
warning: there was one feature warning; re-run with -feature for details
processExists: (pid: Int)Boolean

scala> val pid = 8238
pid: Int = 8238

scala> processExists(pid)
res11: Boolean = true

scala> processExists(1234)
res12: Boolean = false

How to check if a process id (PID) exists, To check for the existence of a process, use is the exit code will be non-zero even if the pid is running and you dont have permission to kill it. -eq 0 ]; then echo "$pid is currently running, not executing $0 twice, exiting now. Note: If you have more than one process ID, you may have trouble identifying the parent process ID when using the pidof & pgrep command. Method-3: How to find the Process ID (PID) of a program running on Linux using the pstree Command. The pstree command shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted.

You can import sys.process._

General example
import sys.process._
scala> import sys.process._
import sys.process._

scala> "ps" !

PID TTY           TIME CMD
570 ttys000    0:00.02 -bash
591 ttys000    0:00.01 bash /usr/local/bin/scala
getting the PID for the process scala
// !! to get result as String
scala> "\\d+".r.findFirstIn( "ps" #| "grep /usr/local/bin/scala" !! )
res9: Option[String] = Some(591)

for a more information see: http://www.scala-lang.org/api/current/index.html#scala.sys.process.package

Processes in Linux/Unix, For example, pwd when issued which is used to list the current directory location the For a running program (named process) Pidof finds the process id's (pids). You want to use another directory as the base directory when running an external command in Scala. Solution Use one of the Process factory methods, setting your command and the desired directory, then running the process with the usual ! or !! commands.

Process API Updates in Java, Suppose if we want the process id of current running process, or we want to The process class is an old class in Process API, only some methods like, pid(),  Each process running in Microsoft Windows is assigned a unique decimal number called the process ID (PID). This number is used to specify the process when attaching a debugger to it. You can determine the PID for a given app using Task Manager, the tasklist command, the TList utility, or the debugger.

Guide to java.lang.Process API, Become a writer on the site, in the Computer Science, Scala, Linux and Kotlin areas. We're not Using Process Class for Compiling and Running Java Program. Let's see an We first need to find out the process ID of the current running process by checking the task manager and find out the pid. Let's see  In theory the only person who can reliably use a pid is the processes parent, as once a process dies, it's pid can get reused by another process. Then once wait/waitpid has returned that pid it is no longer valid. In some systems pids range from 2 to 32767, if you had a webserver running one CGI program a second,

Process API, Monitor commands: Monitor long-running processes and restart them if they terminate Command, Strings that specify the external program file to call and its arguments, if any. This is initially a copy of the system environment of the current process. The method Process.pid returns the native process ID of the process. In the previous example you learned how to obtain the query ID and process ID (PID) for a completed query from the SVL_QLOG view. You might need to find the PID for a query that is still running. For example, you will need the PID if you need to cancel a query that is taking too long to run.

Scala Standard Library 2.13.2, Running an external command can be as simple as "ls".! , or as complex as One cannot, for instance, retrieve a process id for whatever is executing. _ // This uses ! to get the exit code def fileExists(name: String) = Seq("test", "-f", name)​. Unfortunately, if a stored proc is currently running, it shows the full test of the SP, not which command within the proc is executing.

Comments
  • or check existence of /proc/{pid} serverfault.com/questions/366474/… (not sure about scala but should be a 1 liner via regular file.exists api)
  • @zapl Thank you. How does this solution compare to parsing the ps command?
  • @zapl Note, that checking /proc/{pid} will be strictly linux solution (e.g. it won't work on osx)
  • @DannyBit it's easier I guess. But I don't know whether you end up with code that works on more systems either way. ps may not be available or have odd syntax (=parsing fail?) on some system, but the same thing could maybe happen to /proc/{pid} on some *nix/bsd/odd thing. The only thing I know for sure is that it works on my machine.
  • look at Ammonite
  • Yes I am on an Unix machine. What would the cleanest way to parse ps in Scala?
  • Check for updated answer with def processExists(pid: Int)