I am trying to start a daemon on a server with Ajax PHP and JQuery. The following code executes without any errors an opens all the html files in separate browser windows like it's supposed to. I receive the alert message . However, the daemon never starts. Can anyone see what is wrong with the code?

Server side php file:

filename: executedp.php

<?php
$startDaemon = exec('sh ~/.devilspie/ruleset.sh $x; done');
?>

Client side JQuery Code:

if(found) {
                        //call to start daemon
			var xhr = $.ajax({ 
			url: 'executedp.php',
 			success: alert("It has begun!")  
			}); 
		
			switch($.tableCount) {
			
			case ($.tableCount = 1):
			window.open('table1.html');
			xhr;
			break;			
			
			case ($.tableCount = 2):
			window.open('table1.html');
			window.open('table2.html');
			xhr;
			break;
			
			case ($.tableCount = 3):
			window.open('table1.html');
			window.open('table2.html');
			window.open('table3.html');
			xhr;
			break;

			case ($.tableCount = 4):
			window.open('table1.html');
			window.open('table2.html');
			window.open('table3.html');
			window.open('table4.html');
			xhr;
			break;
			}
            }

I have researched for hours and to high heaven for a resolution. Any suggestions greatly appreciated.

Dani AI

Generated

The AJAX call can legitimately return success while the daemon never runs. Common root causes are: the command is being invoked with the wrong environment (relative paths, no DISPLAY, no X credentials), PHP configuration or OS policy blocking exec, the web user lacking permission to start GUI processes, and the command being started in the foreground so Apache/nginx waits for it and then the process dies. These are separate problems and should be checked individually.

Quick, practical checklist and fixes:

  • Run the exact script from the webserver account to reproduce the environment (for example, run it as the web user). Confirm the script runs when started manually and note full absolute paths for the shell and script.
  • Avoid tilde/home shortcuts inside web-invoked shells; use absolute paths and the full interpreter path. Background the process and redirect output so it doesn’t get killed: for example, from PHP call the script with nohup and redirect stderr/stdout to a log, then background with &:
    exec('nohup /full/path/to/ruleset.sh > /tmp/ruleset.log 2>&1 &');
  • Capture command output and exit code while testing to see failures:
    exec('/full/path/to/ruleset.sh 2>&1', $output, $return_var);
  • Verify PHP settings and OS policies: disable_functions/safe_mode (older PHP5), SELinux/AppArmor, and filesystem permissions can stop exec. Also GUI apps need X credentials (DISPLAY/XAUTHORITY) and generally must be launched as the logged-in user — launching browsers from www-data will usually fail.

A few higher-level recommendations: don’t use a web request to spawn long-running GUI daemons directly. Use a system service (systemd or a supervisor), a dedicated background worker, or a small privileged wrapper (sudoers NOPASSWD carefully limited) that launches the user session process. Tying back to earlier replies: permission checks are essential (), and forking/backgrounding is a valid approach but often better handled outside the web SAPI than with pcntl in a web process (). Finally, avoid exposing arbitrary shell execution from web code for security reasons.

Recommended Answers

All 2 Replies

Are you sure that the web user (which runs PHP) has rights to start an external application ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.