tag:blogger.com,1999:blog-5548378134220649958.post5645447335551620187..comments2023-11-13T08:30:58.198+00:00Comments on Argh!: PHP and long running processesColin McKinnonhttp://www.blogger.com/profile/01092917089487772988noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-5548378134220649958.post-91658135764617910812022-06-24T21:28:40.862+01:002022-06-24T21:28:40.862+01:00I am running MAMP on a Mac. When I try to run the...I am running MAMP on a Mac. When I try to run the exec command, I get a message saying that at cannot open the .lockfile. The command runs fine from terminal.<br /><br />Any ideas why it doesn't work from the exec command?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-72754365743375903522020-12-04T21:35:03.570+00:002020-12-04T21:35:03.570+00:00nohup is very portable and does not require the &#...nohup is very portable and does not require the 'at' package to be installed. 'at' is not installed by default and may not be available on some hosting providers without some more expensive options. A nohup spawned process by default has the PPID of the calling shell, but automatically gets assigned to '1' when the calling process exits. By adding the & to the end of the line, you are asking for it to be a background only process. Being a background process, the calling process is no longer blocked by the child process and can run exit/return immediately. I believe that is what you are experiencing. Try running nohup on a php script without the & and use another terminal to see the process PPID. Next, kill the parent process, and you will see it revert to 1.Samhttps://www.blogger.com/profile/06699462541455665872noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-35545131917033288402018-09-05T21:58:40.998+01:002018-09-05T21:58:40.998+01:00Ah well, that's another discussion altogether ...Ah well, that's another discussion altogether - indeed its actually 2 different discussions - one about limiting the length of time a process runs for and one about handling the output and/or early termination of a process.Colin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-17002493019454234342018-04-19T09:01:56.521+01:002018-04-19T09:01:56.521+01:00what will happen if longThing.php execution takes ...what will happen if longThing.php execution takes long period time and crashed ?Minhhttps://www.blogger.com/profile/17890469993742076856noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-13645437448868716472017-09-04T16:03:04.026+01:002017-09-04T16:03:04.026+01:00But note that the targeted policy on RHEL 6 blocks...But note that the targeted policy on RHEL 6 blocks access to this file for system_u. Yes, SELinux is blocking the use of another security enforcement system. http://symcbean.blogspot.co.uk/2016/11/selinux-sucks.htmlColin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-91196709598312842862017-01-06T05:03:54.065+00:002017-01-06T05:03:54.065+00:00How does it differ from executing a long running s...How does it differ from executing a long running script with cron job?Anonymoushttps://www.blogger.com/profile/07831938887575731446noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-69226680058262902702016-12-10T23:20:30.958+00:002016-12-10T23:20:30.958+00:00Actually you would want to reset the flag as soon ...Actually you would want to reset the flag as soon as your agent starts processing it (to prevent the next iteration of the agent starting to process the same job). You don't need to explitily record the fact that an agent has started processing it since this is represented by the agent's thread of execution (OK, so the second agent could check an earlier instance was still running - but this is a lot more complex).<br /><br />(ultimately this is the same solution as I've described - handing the task over to another an existing daemon for action)Colin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-89800443873931474662015-10-20T22:31:05.743+01:002015-10-20T22:31:05.743+01:00I found this to be a good solution, but as the abo...I found this to be a good solution, but as the above comment, my hoster doesn't permit me or apache to use at. <br /><br />What in your opinion is the next best way to launch a long php process? I'm looking at it and am leaning towards setting a flag which a cron job checks to start the job, then unsetting it when the job is completed. In other words, set flag, have bash check flag ever x minutes/hours, then start job if A: flag is set and B: job is not running (easy to determine via ps aux). I'm not fond at all of using & for processes, I've had too many issues with that over the years.techienoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-30653634434791605952015-01-28T20:50:54.439+00:002015-01-28T20:50:54.439+00:00Sorry - there's no (legitimate) way to get aro...Sorry - there's no (legitimate) way to get around this - your ISP has configured the webserver uid with a shell which does not allow execution of commands (in this case /bin/nologin)Colin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-67583868462035855682015-01-27T11:15:47.419+00:002015-01-27T11:15:47.419+00:00This works when I execute the command from the com...This works when I execute the command from the command line, but when I try it via a script I get "This account is currently not available."<br /><br />There are no users in at.deny<br /><br />Is the reason I am getting the message because Apache is not allowed to execute commands ? See passwd file :apache:x:48:48:Apache:/var/www:/sbin/nologin<br /><br />I could change that, but is allowing this not a security risk ?<br /><br />Can I submit the command using another user ?Fliphttps://www.blogger.com/profile/06312075424661014093noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-82850796119546314462014-06-10T21:19:47.807+01:002014-06-10T21:19:47.807+01:00Your welcome. Note that you'll run into the sa...Your welcome. Note that you'll run into the same problems if you try to call fork() from mod_perl (and the CLI PHP SAPI will safely fork() and setsid())Colin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-27555689306993298702014-05-23T17:28:36.510+01:002014-05-23T17:28:36.510+01:00I Wish I had seen this four years ago when you cre...I Wish I had seen this four years ago when you created this. My solution in the past has been to appeal to a forking language like perl. I would use PHP's system call to call a perl script that forks. In the forked process, I would either just do the work, or use LWP to call the desired php script (for that rare occasion where you need to whole web server infrastructure available).<br /><br />Thanks......Anonymoushttps://www.blogger.com/profile/15597757487839643661noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-37502877068660466862014-03-31T14:34:17.920+01:002014-03-31T14:34:17.920+01:00How to do this if the web server is in a shared ho...How to do this if the web server is in a shared hosting ?Anonymoushttps://www.blogger.com/profile/05422240967405142507noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-73835802163501665932013-08-16T23:57:52.186+01:002013-08-16T23:57:52.186+01:00http://linux.die.net/man/5/at.allowhttp://linux.die.net/man/5/at.allowColin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-83754196350452054962013-08-09T10:22:08.325+01:002013-08-09T10:22:08.325+01:00And how to fight with "you do not have permis...And how to fight with "you do not have permission to use this program" on www user?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-80363833369448953142013-07-10T15:46:17.846+01:002013-07-10T15:46:17.846+01:00There's a good point here that I'd omitted...There's a good point here that I'd omitted from the discussion so far - it's very common for the PHP uid to be setup with no shell. In the example above it was trivial to bypass, but with (for example) selinux, it won't be so easy - but these restrictions should be in place for a good reason. Using 'sudo' isn't going to help here (but will resolve the case where the PHP uid is not allowed to run 'at' jobs). <br /><br />If you can't get a shell from php without compromising your security, then you need a different solution (e.g. running your own daemon).Colin McKinnonhttps://www.blogger.com/profile/01092917089487772988noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-10265795190446379732013-07-10T10:59:32.445+01:002013-07-10T10:59:32.445+01:00Thanks for great advise.
I spent quit some time b...Thanks for great advise.<br /><br />I spent quit some time battling a gotcha as well.<br /><br />The "at now" returned e.g. "job 30 at 2013-07-10 11:45" but the job was not executed. <br /><br />The problem showed to be caused by that the default SHELL of "at" while being executed by php exec() was <br />/sbin/nologin (found by simply running: echo exec('echo $SHELL')).<br /><br />Assigning a proper shell to "at" solved the issue:<br />exec('echo "mv 1.txt 2.txt" | SHELL=/bin/bash at now 2>&1', $out);<br /><br />PS: Thanks to http://crashingdaily.wordpress.com/tag/shell/tanderupnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-54779091522802183692013-02-05T17:25:46.955+00:002013-02-05T17:25:46.955+00:00Thanks for this. Here's a gotcha to watch out ...Thanks for this. Here's a gotcha to watch out for ...<br /><br />Make sure /etc/at.deny does not contain your web server user.<br /><br />That caught me for a while. On Ubuntu Linux, at.deny contains www-data.Rosshttps://www.blogger.com/profile/07407852000898964352noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-15873963019409977632013-01-17T01:17:06.307+00:002013-01-17T01:17:06.307+00:00Hi, thanks for posting your solution. is there a w...Hi, thanks for posting your solution. is there a windows equivalent? For something running on WAMP.gillbateshttps://www.blogger.com/profile/15497542780824296320noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-60676839724305163662012-03-13T07:52:42.540+00:002012-03-13T07:52:42.540+00:00The 30 seconds is customizable in /System/Library/...The 30 seconds is customizable in /System/Library/LaunchDaemons/com.apple.atrun.plist<br /><br />To activate it : <br /># launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plistAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-15120749635128204042012-02-17T02:04:44.091+00:002012-02-17T02:04:44.091+00:00Ditto...been trying to spawn a process from a web ...Ditto...been trying to spawn a process from a web script written in tcl (using tcl's exec), but everything I tried (nohup, redirection etc) halts the Apache thread until the child is finished. Only the 'at' command worked as expected. ThanksGertjanhttps://www.blogger.com/profile/09116403018558105513noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-69318892520262002242011-10-13T11:49:21.258+01:002011-10-13T11:49:21.258+01:00U have made it simple with the best explanation my...U have made it simple with the best explanation my fren.Thanks for ur best points.<br />Cheers !web design companyhttp://www.web-designs-company.comnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-68297918255350585822011-09-10T15:32:15.501+01:002011-09-10T15:32:15.501+01:00You have probably just saved me half a day or a da...You have probably just saved me half a day or a day, while being very close to deadlines. Thank you.ikarihttps://www.blogger.com/profile/11109602562026564535noreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-78138808151872659932011-08-30T13:17:36.802+01:002011-08-30T13:17:36.802+01:00Note that on Mac OS X, 'at' is disabled by...Note that on Mac OS X, 'at' is disabled by default. You can enable it (see the man page for 'atrun') but it still only runs periodically (every 30 seconds by default).Philip Lambnoreply@blogger.comtag:blogger.com,1999:blog-5548378134220649958.post-14593120545441709662011-07-12T23:45:06.233+01:002011-07-12T23:45:06.233+01:00Very interesting article. Thank you for sharing.Very interesting article. Thank you for sharing.Valdez V.https://www.blogger.com/profile/04836927299834193517noreply@blogger.com