0

I want to run a linux command via PHP exec() and my first command below executing in background at Ubuntu Server 22.04 perfectly -

<?php
$cmd = "java -jar /var/www/html/selenium-drivers/selenium-server-4.14.1.jar hub";
exec($cmd . " > /dev/null &");
?> 

But the very similar of my second command is not executing.

<?php
$cmd = "java -jar /var/www/html/selenium-drivers/selenium-server-4.14.1.jar node --port 5555 --selenium-manager true";
exec($cmd . " > /dev/null &");
?> 

I am unable to find out the reason why not working. Anybody please help?

Checked all necessay permissions of PHP and there is not issue of permissions. First command executing perfectly but the second is not.

11
  • try $cmd = "nohup java -jar /var/www/html/selenium-drivers/selenium-server-4.14.1.jar node --port 5555 --selenium-manager true > /dev/null 2>&1 &"; exec($cmd); Commented Nov 3, 2023 at 19:21
  • What are these commands supposed to do? What does the node argument mean for the Java program? Commented Nov 3, 2023 at 19:25
  • 1
    @ChrisHaas But why would it work for one java command but not the other? They're both running the same JAR file, only with different arguments. Commented Nov 3, 2023 at 20:48
  • 1
    @Barmar, I'll be honest and say that I initially included my comment just as a blanket catch-all for common problems. However, looking at the docs the node parameter says "During startup time, the Node will detect the available drivers that it can use from the System PATH", so that's my best guess Commented Nov 3, 2023 at 21:43
  • 1
    One, can you just make the directory manually? Two, check the permissions of the user/group that PHP is running as. Three, try running the jar with —help or similar to see if you can specify a cache folder that you can control. Commented Nov 4, 2023 at 11:40

1 Answer 1

0

I got fixed the issue with help of the comment by @ChrisHaas The actual issue was related to proper permission to www-data (Apache2) to access the directory. I given permission to the directory like -

sudo chmod -R 777 /var/www/html/selenium-drivers

and then I can execute the command

<?php
$cmd = "java -jar /var/www/html/selenium-drivers/selenium-server-4.14.1.jar node --port 5555 --selenium-manager true";
exec($cmd . " > /dev/null &");
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Great you solved the problem (don't forget to accept/upvote your answer so futuer visitors can see it is helpful). Note though that using 777 permissions is dangerous, you should not do that. The user that is running the web server is the one that needs write permission, correct? So only that user needs write permissions.

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.