2

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.

Ram Guru99
  • 175
  • 1
  • 1
  • 14
  • possible duplicate of [shell\_exec() timeout management & exec()](http://stackoverflow.com/questions/3407939/shell-exec-timeout-management-exec) – Wesley Bland Feb 28 '14 at 14:56
  • @WesleyBland both are entirely different as i'm invoking java processes using shell_exec but not using proc_open which he is using. – Ram Guru99 Mar 01 '14 at 09:30

1 Answers1

1

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.

lubik
  • 71
  • 4