2

in my php script which runs as a cron job, I have

foreach($sites as $site)
{
    exec('./wkhtmltoimage-amd64 ' . $site . ' somefile.png');
    exec('./zopflipng -y somefile.png somefile.png');
}

to generate screenshots monthly for each site.

Now, this cron job runs once a month, and in the unlikely event that someone wanted to delete the image while this job ran, I'm not too sure what would happen. Would the image just not get deleted since it's in use? If that's the case, then how do I make it delete after it's no longer in use?

I've thought of a solution where I could just kill the process, but I am unsure of the outcome too. If I use

lsof -t somefile.png

that will give me the PID of the process. With that, I can kill the process within the delete function of the site using

exec('lsof -t somefile.png | kill -15');

After killing, will the original script near the top of this post still go on? Will the job be canceled? Does the cron job error out? I'd like for it to just be able to move on to the next site.

1

I think I would just test to see if the file exists, if it does then flip it. You should be able to test for file existence quite easily from PHP. You could also do it from the shell (likely Bash or Bourne Shell) when you run the function exec().

 exec('[ -f somefile.png ] && ./zopflipng -y somefile.png somefile.png');

Killing the exec

To address your other aspect to your question. If you're asking what happens when you kill one or both of the exec's through one of he iterations of the for loop in your PHP program, then the answer would be that your for loop should solider on and continue with the next iteration without any issues.

The exec's are separate processes from your PHP script, so unless you exec them and do something with the status codes they return, the caller should be none the wiser that they either finished or were killed.

2
  • The problem is that compression is a long running process. I'm afraid a user will need to delete their site while it is compressing. I'm trying to figure out the best way to delete a file while it's in use without breaking or halting the cron job.
    – No_name
    Jan 23 '14 at 10:33
  • @No_name myabe in that case your backup script should listen to a variable or file a user could interact with and if the signal delete file is send abord the ziping and quit properly the cron task.
    – Kiwy
    Jan 24 '14 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.