Solved

PHP script determine if running a daemon or console

Posted on 2014-09-22
7
285 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

830 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