PHP script determine if running a daemon or console

Posted on 2014-09-22
Last Modified: 2014-09-23
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.
Question by:jaym25
  • 4
  • 3
LVL 34

Expert Comment

ID: 40338302

Author Comment

ID: 40338324
getopt() appears to be a very powerful function with unlimited options.  Would you happen to have an example with the correct parameters or options?
LVL 34

Accepted Solution

gr8gonzo earned 500 total points
ID: 40338363
$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.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 34

Expert Comment

ID: 40338364
Sorry. My phone auto corrected incorrectly.  

"...will be false."

Author Comment

ID: 40338400
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?

Author Comment

ID: 40338403
where $DAEMON is the path to the script
LVL 34

Expert Comment

ID: 40339257
PID=`$DAEMON -d > /dev/null 2>&1 & echo $!`

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql query on a varchar that is numeric. 8 26
unset shopping cart session 15 29
PHP: concatenate query 12 35
PHP get array item with custom id 4 21
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now