0

I have a php script that creates a shell script which is run after making it from the same php file, the shell script generates a registry file that I need to read after the script is executed, again from the same php. The php reads the file, but I think it does it before the file is filled or created, if I go back at the browser and execute the php again, then there is content at the textarea. I have tried to solve it adding sleep(), exit() functions and some other strategies but no success. Here are some of the things I've tried:

// Creation of the shell script: Corpus alignment target to origin
......
$cmd = "cwb-align-encode -r $REGDIR -D $CORPUSLOCATION$corpusname/$corpusname"._."$lang_tg.align\n\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);

// Run the corpus indexation script
$cmd = "/bin/bash  $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);

Read the registry file from the same php:

// 1st try: no content at the textarea
echo "<textarea id='txtArea'>".htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";

// 2nd try: no content at the textarea
echo "<textarea id='txtArea'>".sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";

// 3rd try: no content at the textarea
echo "<textarea id='txtArea'>".exit(); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";

// 4th try: no content at the textarea
echo "<textarea id='txtArea'>".if(filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } else { exit(0); sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); }."</textarea>";

2 Answers 2

1

The command line you are using created a new thread that performs the task. PHP wont wait for it, as you do not refer the strout to php (but to /dev/null)

So, by changing the command, you can make PHP wait and thus get the result you expect.

Now I don't know for sure what the correct command is, but I would start with something like

$cmd = "/bin/bash  $scriptfile"

Also have a look here. You want the opposite of what that guy wants. It does however give a bit more information about what the command actually does.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, as simple as taking out the ` > /dev/null 2>&1 &` from the shell script execution line. Thanks a lot.
0

Even though the answer given by @Jeffrey is the right one, I realized that this answer is only good enough if the shell script takes short execution time, otherwise your php webpage can expire or hang up, so I gave another try with another php function: header('Refresh: x'), and that made it work right!

So here's what I get now:

// Run the corpus indexation script
$cmd = "/bin/bash  $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);

<textarea id="txtArea" rows="28"><?php if (filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); }
else { header('Refresh: 0.5'); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ));} ?></textarea>

UPDATE

Yet another solution:

do { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } while (filesize($REGDIR.$corpusname) == 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.