Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

PHP script determine if running a daemon or console

I have a php script that I can either run from the command line or as a Linux daemon.  I would like to create a conditional statement based on which way it is being run.  Ex:

If (needed function)
    echo("I'm running in daemon mode");
else echo{"I'm running in console mode");

I'm looking for the needed function to accomplish this from within the script.  thank you.
0
jaym25
Asked:
jaym25
  • 4
  • 3
1 Solution
 
gr8gonzoConsultantCommented:
0
 
jaym25Author Commented:
getopt() appears to be a very powerful function with unlimited options.  Would you happen to have an example with the correct parameters or options?
0
 
gr8gonzoConsultantCommented:
$options = getopt ("d");
$daemon_mode = isset ($options ["d"]);

So if you run your script with a -d parameter, $daemon_mode will be true. Otherwise it will be fails.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
gr8gonzoConsultantCommented:
Sorry. My phone auto corrected incorrectly.  

"...will be false."
0
 
jaym25Author Commented:
I thought that was it.  but when I try this code it always shows as false...  
The command I use to enter daemon mode is:  
PID=`$DAEMON > /dev/null 2>&1 & echo $!`  
Perhaps I could add a parameter to this command line?
0
 
jaym25Author Commented:
where $DAEMON is the path to the script
0
 
gr8gonzoConsultantCommented:
PID=`$DAEMON -d > /dev/null 2>&1 & echo $!`
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now