0

I am converting file MRXS to TIFF. It takes sometimes. I want to call another shell_exec after this task finish. But I don't know when the conversion is done.

shell_exec("vips openslideload /tmpSlides/" . $path . " /tmpSlides/test.tiff[tile,compression=lzw]");

This command starts to conversion task. Create an empty tiff file and that tiff file size is increasing till task finish.

I want to check, that file is in use or not? If this file is not in use, I will understand task finished. Or is there any method to check task finish or not?

I want to write something like this :

if(RUN_UNTIL_CONVERTION_DONE("/tmpSlides/test.tiff")) {
 shel_exec(
"kdu_compress.exe -i /tmpSlides/test.tiff -o /tmpSlides/test.jp2 Creversible=yes -rate 0.25 Clayers=1 Clevels=7 Cprecincts={256,256} Cblk={64,64} Corder=RPCL Cuse_sop=yes Stiles={1024,1024} ORGgen_plt=yes ORGtparts=R");
}
Sumithran
  • 6,217
  • 4
  • 40
  • 54
Nevermore
  • 1,663
  • 7
  • 30
  • 56
  • Do you wanna check if the task is finished or the file is being actively written to? – Mason Stedman Oct 18 '18 at 09:57
  • yes, i want to control file is being actively written. – Nevermore Oct 18 '18 at 09:58
  • So just starter, php is a terrible shell controller, I hope this is at least CLI driven. Secondly I would store a started \ ended var somewhere for the processes vs trying to look at active writing\accessing regardless because back to point A. That lets you manage hung processes better (vs just seeing that the process is still "writing the file) – Mason Stedman Oct 18 '18 at 10:04
  • Hello @MasonStedman, my scenario like this : User upload .mrxs file and i convert to .tiff and after tiff conversion, convert to .jp2 .. I can do first conversion easily, but i dont get any response from conversion (is finish, or not). .mrxs -> tiff ---------- tiff -> jp2 – Nevermore Oct 18 '18 at 10:07
  • There's a few tricks you can use there? Do you have \ can make an API that could accept a response available? I can show ya 2 neat ways to do this if so – Mason Stedman Oct 18 '18 at 10:10

1 Answers1

1

Basically it depends over your operating system. Unixes and Windows use different ways to check that, so I will show 2 cases:

Windows

For Windows, there is a program that exists called Handle that will show you which program uses your file. Install it, and then in PHP, use shell_exec('handle <your file>') and parse the output to check if your vips program still is modifying the file. You can even use the -p option in Handle to limit the check to the vips process.

Unix

On Unix-derived systems (POSIX), you can use a built-in tool called fuser to do that:

fuser <your file>

Then, as for Windows, shell_exec this command and parse the result to check if your file has finished being edited.

Another way

Another way to do it is to check if the process is still executing. For that on Linux you can use ps aux with a grep piped in to filter the process you're looking for and check if it is still running. Unfortunately, there's no such way available in Windows as tasklist doesn't show the options given to the launched processes.

linkboss
  • 656
  • 4
  • 7
  • none of this will work using shell_exec w\ php /sayin – Mason Stedman Oct 18 '18 at 10:19
  • Well, it all depends of the rights level the process is running on. On Windows it has to be run with administrative privileges (then it has to be a CLI script, nobody with a sane mind would run a webserver with admin privileges), and on Linux, it needs to have the correct access to the files being edited. I would advise to run it as a CLI script anyway, as it is more secure to have something exec'ing system commands to be isolated from the outside. – linkboss Oct 18 '18 at 10:28
  • Not just that PhP will die on error, die on warning, die on process split, die on a lotta other weird stuff. It's not a great controller for bash\py\external cli stuff. And it exits all of those with an empty return so you can't even see the problem. – Mason Stedman Oct 18 '18 at 10:30
  • Do you have some documentation on that ? I never encountered such a problem in my usage, and I would like to read more about it. – linkboss Oct 18 '18 at 10:38
  • I run into it a ton when people want to control py scrapers via API. Any time something run via shell exec generates output that isn't going through stdout, it will exit with a null return (including warmings and errors). Two second google describing: https://stackoverflow.com/questions/18241305/shell-exec-returning-null-on-ls . Rerouting output can sorta fix if it's not a "bad" error but stil generally results in just dead response in tested code when issues happen.. Also has no idea how to handle things that spawn other processes,.. – Mason Stedman Oct 18 '18 at 10:46
  • Regardless there's easier ways to verify process start\end times that won't leave you potentially hung up than writing a messy series of shell handlers. Why not just have a curl myapi.com/?step=1 etc after every conversion if you're able to run a chain of commands already. No reason to use a complex thing, vs just having something ping your script every step and tell you how far along it is. – Mason Stedman Oct 18 '18 at 10:48