Ask Your Question

puppet onlyif with pgrep does not work

asked 2013-11-22 02:27:41 -0600

spylam gravatar image

updated 2013-11-22 16:11:29 -0600

Ancillas gravatar image

I want to use an exec in puppet onlyif its process is NOT running

exec { "execute me": 
  onlyif  => "pgrep -fc 'ruby execute.rb'",
  command => "execute me", 

So on the above case, if the process 'ruby execute.rb' is already running, the exec should not run as

pgrep -fc 'ruby execute.rb' will return 1.


The command seems to always run in puppet.

On my linux box when I do pgrep -fc 'ruby execute.rb' I always get a value more than 0.

Is there something wrong with my command?

Thank you for your help.

edit retag flag offensive close merge delete


Is pgrep being executed properly? The command isn't fully qualified. Do you set a global path for Exec resources somewhere else?

Ancillas gravatar imageAncillas ( 2013-11-22 16:16:29 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-11-24 20:11:51 -0600

Stefan gravatar image

The onlyif and unless statements do not check the output of a command, they only check the error code.

man pgrep tells us

       0      One or more processes matched the criteria.
       1      No processes matched.
       2      Syntax error in the command line.
       3      Fatal error: out of memory etc.

so if your program is already running, the output of your command will be 1 (because -c counts the number of processes) but the output is discarded. The exitcode will be zero, because a command can be found. As a result the exec resource triggers. You want to use ... (more)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-11-22 02:27:41 -0600

Seen: 2,475 times

Last updated: Nov 26 '13