Question

I have developed an online java code editor at http://joomla5.guru99.com/try-java-editor.html I am invoking javac using shell_exec function of php and executing java code.

$result = shell_exec('javac' .$soucejavafile. '2>&1');
and running classfile by
$result= shell_exec('java' .$classfile. '2>&1');

Now for security purpose, I want to set time limit for this java code execution. For example, java code execution should be stopped after some amount time and all it's processes must be killed

I have tried ulimit and ps commands but couldn't able to achieve this.

Please assist me in the correct direction and please help me to make this possible.

Regards.

Was it helpful?

Solution

You can do it in 3 ways:

1) Call pcntl_fork in PHP and check the timeout in parent process. Kill it if it exceeds using linux kill command.

2) Include timeout in a bash script that you will invoke using shell_exec, see this example: http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3

3) Use proc_open / proc_terminate functions

Personally I would go with number 3, it's the cleanest. If you need quick and dirty, use number 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top