Linux screen managment on root through php user
I want to make a script that would run something in screen
on root 开发者_运维百科user. This has to be done through php system()
function therefore I need to find out way to sudo to root and pass a password, all using PHP.
If you really need to sudo
from PHP (not recommended), it's best to only allow specific commands and not require password for them.
For example, if PHP is running as the apache
user, and you need to run /usr/bin/myapp
, you could add the following to /etc/sudoers
(or wherever sudoers is):
apache ALL = (root) NOPASSWD:NOEXEC: /usr/bin/myapp
This means that user apache
can run /usr/bin/myapp
as root
without password, but the app can't execute anything else.
I'm sure there must be a better way to do whatever it is you're trying to accomplish than whatever mechanism you're trying to create.
If you simply want to write messages from a php script to a single screen
session somewhere, try this:
In php
Open a file with append-write access:
$handle = fopen("/var/log/from_php", "wb");
Write to your file:
fwrite($handle, "Sold another unit to " . $customer . "\n");
In your screen session
tail -F /var/log/from_php
If you can't just run tail
in a screen session, you can use the write(1)
utility to write messages to different terminals. See write(1)
and mesg(1)
for details on this mechanism. (I don't like it as much as the logfile approach, because that is durable and can be searched later. But I don't know exactly what you're trying to accomplish, so this is another option that might work better than tail -F
on a log file.)
精彩评论