Source Matters
Source Matters

Reputation: 1221

How to properly set up php script in systemd service?

SO, I'm trying to set up a service using systemd on an Axis camera (I believe it's Arch linux or a very stripped down Linux version that's custom). I have the service set up as follows:

myservice.service:

[Unit]
Description=Ping Monitoring Daemon
After=network.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/listen-start.sh
ExecStop=/etc/init.d/listen-stop.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Here is listen-start.sh:

#!/bin/sh
echo $(curl http://website.com/logssomethingtoadatabase.php?action=start)

My php script:

<?php
echo 'Service Started';

while (true) {
$foo = print_r($_SERVER, true);

$blnStop = false;

if ( $_GET['action'] === 'start') {
    mail("[email protected]","Debug", "Camera service started with current values:\r\n\r\n" . $foo);
} else {
    mail("[email protected]","Debug", "Camera service stopped with current values:\r\n\r\n" . $foo);

    $blnStop = true;
}

if ( $blnStop ) {
    break 2;
}

sleep(60);
}

echo 'Service Stopped';
die();
?>

Issue I'm encountering is once I start the service, I'm unable to "stop it", even after issuing the "systemctl stop" and "disable" commands on it. I have to power cycle the camera to get it to stop emailing me. I can't seem to get it to "break" out of the while loop.

The goal here is to call that script every 60 seconds which is a device monitoring effort. I must use systemd in this case as that's all that the camera supports.

Additional info: The systemctl start and stop commands work successfully, and the script itself runs fine.

Can anyone see anything I may be missing?

Upvotes: 1

Views: 2558

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

the listen-stop.sh script is probably calling the php script with action=stop, right? probably like:

#!/bin/sh
echo $(curl http://website.com/logssomethingtoadatabase.php?action=stop)

what you are trying to achieve in that while (true) loop will not work, because the php script that got triggered by the start call, will always have $_GET['action'] = 'start'. your subsequent execution of the systemctl stop will not affect that already running php script, this is why it never ends executing.

QUESTION: is that php script supposed to do anything in regards to the camera? or just send the mail notification that service started/stopped?

if its only the mail notification, then all you need to have in that php script is:

<?php

if ( $_GET['action'] === 'start') {
    mail("[email protected]","Debug", "Camera service started with current values:\r\n\r\n" . $foo);
    echo 'Service Started';
} else {
    mail("[email protected]","Debug", "Camera service stopped with current values:\r\n\r\n" . $foo);
    echo 'Service Stopped';
}

?>

TL;DR: you have written the script in a similar mode like a bash daemon script, but thats not how it works. on each execution of the php script, a new instance is created, so you cant "talk" to the instance you created when you executed the systemctl start.

Upvotes: 0

Related Questions