1

I want to use the pidof by a process given by name in tcl. I have used [exec pidof $proc_name ], but it always returns an error: child process exited abnormally.

I read somewhere exec always treat non-zero return as error as pidof return the process id number. Does anyone know if there is a workaround? Thanks in advance!

I want to use pidof is that i want to see if that process is running if not i will restart the process.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
Shuqin Li-Kokko
  • 141
  • 1
  • 1
  • 7
  • If I use command line ps -C $proc_name ([exec ps -C $proc_name]), the result is the same. – Shuqin Li-Kokko Sep 17 '15 at 11:22
  • I have the code such as "set rc [catch {exec pgrep $proc_name} ]" or "set rc set rc [catch {pidof $proc_name} ] " or "set rc [catch {exec ps -C $proc_name} ]". No mater the process is running or not, rc always gets value 1. So there is not way for me to know if the process is running or not. It works for some people here but not for me. It looks like an OS distribution issue. My os is centos "2.6.18-274.el5PAE #1 SMP" – Shuqin Li-Kokko Sep 18 '15 at 07:40
  • I also read this article "http://stackoverflow.com/questions/2903354/bash-script-to-check-running-process". Under bash, it always returns value 1. Does anyone have any ideas? – Shuqin Li-Kokko Sep 18 '15 at 07:59
  • So the bash versions of my two computers are GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) and GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu) – Shuqin Li-Kokko Sep 18 '15 at 08:06
  • If I have #!/bin/bash -e, it doesn't print out anything. seems script doesn't get executed.#!/bin/bash SERVICE=$1 ps -a | grep -v grep | grep $1 > /dev/null result=$? echo "exit code: ${result}" if [ "${result}" -eq "0" ] ; then echo "`date`: $SERVICE service running, everything is fine" else echo "`date`: $SERVICE is not running" fi – Shuqin Li-Kokko Sep 18 '15 at 08:16

2 Answers2

1

The problem is that pidof does strange things with exit codes:

Exit Status

  1. At least one program was found with the requested name.
  2. No program was found with the requested name.

This interacts badly with exec which treats a non-zero exit code as indicating that it should tell the rest of Tcl that there was an error.

The simplest way of dealing with this is a little extra shell script wrapper. Let's hide it inside a procedure for convenience:

proc pidof {name} {
    exec /bin/bash -c "pidof '$name'; exit \$(( \$? - 1 ))"
}

All that does is subtract 1 from the exit code before it hits back into Tcl.

(You could also fix this using the techniques described in the exec manual but I think it's simpler to fix on the bash side this time.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Warning: untested. Can't ssh into my linux box to check right now. :-) – Donal Fellows Sep 17 '15 at 12:39
  • Thanks. It doesn't work. When I try to print the result of pidof procedure, it produces empty lines. Don't know what value is returns. – Shuqin Li-Kokko Sep 17 '15 at 12:58
  • I have also tried "catch", it always returns 1 when process is running or not. – Shuqin Li-Kokko Sep 17 '15 at 13:01
  • Interesting. The manual page for my version of 'pidof' has 0 and 1 as return codes (LinuxMint 17.2). I think I would stick with 'ps -C'. – Brad Lanam Sep 17 '15 at 14:13
  • `set val [catch {exec ps -C $name}]; return $val` should work. If you still get a return value of 1, you need to check exactly how your program is being executed and make sure the `ps -C` is finding it when it is running. – Brad Lanam Sep 17 '15 at 14:24
  • Under shell, PS -C runs correctly. The only problem is with TCL. – Shuqin Li-Kokko Sep 17 '15 at 18:18
  • The code I posted in the comment works correctly. You need the 'catch', otherwise when the process is not there, you will get that error message. Can you edit your question and add the actual code you are using so we can inspect it? – Brad Lanam Sep 17 '15 at 19:30
  • The ps -C $proc_name always returns 1 no matter the process is running or not. Running the command under shell gives correct result -- process id of the process or nothing if it is not running. – Shuqin Li-Kokko Sep 18 '15 at 07:11
  • What does `echo $?` show when running ps -C from the command line? – Brad Lanam Sep 19 '15 at 19:28
  • On shell, it returns value correctly. It returns zero when process is running; and value 1 when process is not running. But running TCL script, doesn't get the correct return value. – Shuqin Li-Kokko Sep 21 '15 at 07:39
  • When process is running [root@hub1 ~]# ps -C ProviderServer PID TTY TIME CMD 4138 ? 00:12:11 ProviderServer [root@hub1 ~]# echo $? 0 When process is not running, [root@hub1 ~]# ps -C ProviderxServer PID TTY TIME CMD [root@hub1 ~]# echo $? 1 – Shuqin Li-Kokko Sep 21 '15 at 07:40
-1

I ran into this and ended up causing some issues with the old linux environment I run in (no bash and exit code handling was a bit different with busybox).

My solution that should work anywhere would be similar to what a few suggested:

proc pidof {name} {
  catch {exec -ignorestderr -- pidof $name} pid
  if {[string is entier -strict $pid]} {
    return $pid 
  }
}