0

I use Mac OS X and use Launchd to automatically start XAMPP. I have set the apache user set to be the same as my account name in httpd.conf

User cwd
Group nogroup

Well, if I just let the computer start up, and if I run a php script with these commands, here is what I get:

echo exec('whoami'); //cwd
exec('echo 1234 | pbcopy');
echo exec('pbpaste'); // (nothing)

However, if I stop XAMPP and restart it (using XAMPP Control), then it works as expected and the third line's result is 1234.

echo exec('pbpaste'); // 1234

I also have similar experiences with running other system commands with exec or shell_exec like ssh. They don't seem to work until I manually restart apache. Does anyone have anythoughts on this?

Here is my launch config file:

/Library/LaunchDaemons/xampp.sql.startapache.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnableTransactions</key>
<true/>
<key>Label</key>
<string>xampp.startapache.mysql</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/XAMPP/xamppfiles/xampp</string>
<string>startmysql</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/Applications/XAMPP/xamppfiles</string>
<key>KeepAlive</key>
<false/>
<key>AbandonProcessGroup</key>
<true/>
</dict>
</plist>

1 Answer 1

2

It's not a privilege thing, it's because pbcopy and pbpaste need a pasteboard server (essentially, a background program that holds the pasteboard), and that's run as part of a user login session. launchd runs apache in a system context, so there's no pasteboard server available (note that even though apache switches users to cwd, that's not the same as joining your login session).

There might be a way to join a particular login session (involving launchctl bsexec), but for a web service this would be a bad idea. First, because it wouldn't be able to join until you logged in, and would break as soon as you logged out; web service really should run independently of who happens to be logged in at the time. Second, because it would mean your php scripts are sharing a pasteboard with your user session -- you'd go to copy something from one program to another, and find you were pasting in something from the web script, not what you'd copied. And the same thing could happen to the script -- it pbcopies something, then pbpastes back something completely different because you happened to use copy from the GUI. And if multiple copies of the script were running they could step on each other... it'd be a complete mess. I'm not sure what you're trying to accomplish with pbcopy and pbpaste, but I'm pretty sure that's the wrong way to do it.

3
  • I run XAMPP on my laptop as a development machine. It's not accessible to the world and so there aren't any security concerns. I also don't care if it is not available after I log out. I literally am using this setup so I can have pbcopy set the clipboard to output from the PHP script, so I can then use the data myself. I would be very interested in knowing how to use the bsexec option to get this to work as needed :)
    – cwd
    Commented Mar 29, 2011 at 4:37
  • It's not going to be particularly easy, since you need to be running as root to use launchctl bsexec, and by the time your PHP script gets run it's already dropped root and is running as cwd. So you'd need to launch the entire apache server within your login session's context. I think you'll have to write a wrapper script that waits for you to log in, then runs `launchctl bsexec $(ps ax | grep [l]oginwindow | cut -c 1-5) /Applications/XAMPP/xamppfiles/xampp startmysql', and modify the launchd.plist to run that script instead of running xampp directly. Commented Mar 29, 2011 at 6:23
  • Thanks, Gordon. Although complicated this explains the phenomenon I was experiencing and helps to point me in the right direction of finding an appropriate solution. For now I've used an applescript which runs at startup (withoutink.com/technology/howto-autostart-xampp-mac-osx) to launch XAMPP. This is not the right way to do it, and the downside is that it has my password hard coded inside the script, but it seems to work. It is much easier than stopping and restarting XAMPP using the XAMPP Control app each time (it works fine when I do that). I will look into using bsexec.
    – cwd
    Commented Mar 29, 2011 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .