cgi script and external UNIX commands (like swadm)

Hi,
I am trying to implement a server monitoring dashboard using cgi scripting. I am planning to run the necessary unix scripts from the web page using cgi. This method works fine for standard unix commands but I am unable to run some external unix commands (like swadm show_processes, swadm count_messages etc). These commands work fine as ordinary shell scripts, but fail in case of cgi scripts. Please help me out in executinf these commands.

They're probably not in your PATH. The PATH of CGI scripts are very minimal by default. Either add the relevant directories to your PATH or call the programs by their absolute paths like /path/to/program

Placing a link to the original in your current path is somewhat more secure than modifying the path itself.

Hi,
I tried giving the full path to the swadm utility and executing it. Still it doesnt work. The script runs fine from bash command prompt, but fails in webpage. Can you please advise me how to set the PATH to new lacation? I am using tomcat server

---------- Post updated at 10:46 AM ---------- Previous update was at 07:20 AM ----------

Could the problem be with the user running the script (the user not having permission to run the command) ?? I couldn't run whoami command also from the cgi. Please share your thoughts/info...

In what way does it "not work"?

Have you tried redirecting stderr somewhere so you can read error messages?

Hi,

Here is the code I am using:

cd /apps/ip06/tibco/iprocess/util
echo `swadm show_processes` > output.txt

"apps/ip06/tibco/iprocess/util" is the location where 'swadm' utility exists. The command works fine in command prompt. but doesnt work as a cgi script, when executed from browser.

It gives no output at all. I get a zero byte 'output.txt' file. I tried writing stderr also. No errors are written.

By full path I don't mean "cd /path/to/application/folder/ ; application". I mean running /path/to/application/folder/appname instead of just appname. This is UNIX, not DOS, being in the same folder doesn't help. It doesn't find things in the current folder by default for security reasons, or anyone could drop a 'swadm' program anywhere and have people run it by accident.

Yes I did try running the full path too. That also doesn't work. Wold the cause be the user which is running the script when it is run using cgi?

It might be.

It'd be nice to get proper error messages, but cgi redirects all that by default... You can get them back in a CGI script like

#!/bin/sh

# Redirects stderr from /dev/null into stdout, so they'll show up in browser
# Comment this when you don't need it anymore!
exec 2>&1

echo "Content-type: text/plain"
echo
echo "'permission denied' should only be shown when exec 2>&1 is done"
touch /

exit 0

Hi,
I found the solution. As per your suggestion, I got the error message and it said, an environment variable was not set. I set the environment variable in my script and now, the swadm command is working fine. Thanks a lot for the help.

1 Like