the script is already running. I tried 'php distribution.php'. the sh: line 2: error comes before the if command. The if command is good. for some reason it echoes 2 lines, like ... $ps = shell_exec("pgrep -fl $script_name |grep -v $my_pid "); is twice. sh: line 2: 27834: command not found 27834 php distribution.php 27834