Solved

PHP script determine if running a daemon or console

Posted on 2014-09-22
7
280 Views
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.
0
Comment
Question by:jaym25
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40338302
0
 

Author Comment

by:jaym25
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?
0
 
LVL 34

Accepted Solution

by:
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40338364
Sorry. My phone auto corrected incorrectly.  

"...will be false."
0
 

Author Comment

by:jaym25
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?
0
 

Author Comment

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

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

776 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