/    Sign up×
Community /Pin to ProfileBookmark

Can a PHP script start a 2nd one, then exit, leaving the 2nd one running?

I’ve set us a system to allow authorized visitors to play private music files. The script essentially creates a temporary directory with a unique name for an authorized user, where the requested music file is placed. (It even builds an .htaccess file to help prevent others from accessing the temp files.). But I need a way to get rid of the temp files and directories after they’re no longer needed. Unfortunately the script cant “sleep() or wait around for time to pass. It is called via an XMLHttpRequest in SYNCHRONOUS mode because it must finish and return a result before the calling page can play the music. So the only thing missing is a way to stop the temp files from hanging around forever.

So since the script must compete and return a result, is it possible for the script to start a 2nd PHP program, passing it the private directory? That second program could then stay in a loop, sleeping for a minute at a time, while checking the last mod date on the directory. When it sees the directory hasn’t been “touched” after a certain time, it can then delete and exit. Essentially a one-time garbage collector for the current user.

So my first question is, is this even possible? How would a PHP script (called from a web page) start another PHP script, without “blocking” or waiting for this 2nd script to exit?

The second question is, if the above is possible, am I doing it a stupid way? I could set up a Cron job I guess, which could look at all directories and files in the “temporary” area, and delete then when they get old. But I just thought that was overkill, since the Chron job would be running all the time, while the first method I’m considering would only run on an “as needed” basis.

Thanks for any thoughts!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 06.2019 — It might be possible with [exec()](https://php.net/exec) to launch it as a background process, e.g. something like...
<i>
</i>exec('nohup /usr/bin/php yourfile.php some_arg &amp;');
// nohup along with "&amp;" run it as a detached background process


PS: I'm assuming a Linux environment on the web server.
Copy linkTweet thisAlerts:
@PeterPan_321authorJun 06.2019 — @NogDog#1604400 Thanks! That seems like what I'm looking for! So its a genuine detached process, not a "child process" that will be stopped when the initiating program exits, correct? And I believe I fetch the arguments with 'C' style argc / argv type methods?

Its good to know this is possible, but I'm still on the horns of a dilemma about using it. yes, it only has to run for a while and then can quit when its done cleaning up. BUT, unlike a cron job, it would seem I have no way to control it if some stupid ,mistake I make causes it to run endlessly! This is, after all, running at a hosting company, so I don't have any tool like a "task manager" to find and stop it. Is there any way to launch such a detached process with some kind of time limit, as a backup?
Copy linkTweet thisAlerts:
@NogDogJun 06.2019 — To the best of my knowledge the nohup...&amp; should leave it running even after the calling script terminates, but I'd have to test it myself before I put much money on it. ;)

I think I might tend to lean toward using a database-driven solution where you put the file name of interest (or whatever makes sense) in the DB, then actually use the cron approach, where the cron job would look for unprocessed entries in the DB and see if the file can be deleted, and if so, mark it as processed in the DB once it's deleted.
Copy linkTweet thisAlerts:
@tracknutJun 06.2019 — If these files are secure, and the timeline for deleting them is not critical, what about deleting them on the next instantiation of the main player program? Ie, player starts up, quickly deletes old stuff from previous users, then plays the music for the guy who wants it.
Copy linkTweet thisAlerts:
@PeterPan_321authorJun 07.2019 — @tracknut well there is a measure of "security by obscurity" going on here. Authorizing a download of music from a secure store is pretty easy, because its all PHP, accessing secure files with known authorization. the problem comes when allowing the visitor to just play songs, with their HTML-5 player. (my skins and such, but still based on an <audio> tag under the hood. ) So for the HTML-5 based player to be able to access the song, it has to temporarily be moved to an un-secure location. Hence the cryptic unique temp directory based on the users credentials, and the script creating an HTACCESS file in the dir, denying everyone but the users current IP. If the visitor returns later from a different IP, his/her credentials will be recognized, and a new HTACCESS file gets written. So that is "pretty" secure. especially considering the auth user can also download files, which of course means he/she could share the file with others whether I like it or not.

So... its not like trying to protect financial records. Its in the "good enough" category. But something in me says it would be nice to do some garbage collection. I'm not a SUPER popular performer, and I don't estimate much more than 1000 authorized users. So that's a mess of dormant directories to look at. Especially considering that once the visitor gets tired of listening via my site, they are going to just download and add the songs to their mobile device music players, So i just seems like it would be good to clean them up, meaning delete whole directories that haven't been touched in a while.

I'm starting to lean toward a cron job though. If I make some dumb mistake with a "run forever" script that i can't stop, and each user generates a new instance that goes amok and eating up server CPU time, the hosting company is going to be upset. LOL!
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...