Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-21-2012, 09:24 AM   #1
Registered: Sep 2012
Posts: 48

Rep: Reputation: Disabled
failed to execute system command from CGI program

I have CGI program (written in C) that runs from apache server (2.22).
As a part of program flow, there are some commands that the program suppose to execute (grep, mutt). When I run this program mnually it works, but when it runs as a URL (https://myhost.mydomain/cgi-bin/myprogram) any system command is not executing without generating error message.
I tried to create some log file where the output and error messages should be written (such as system("/bin/grep XXX file > /tmp/grep.log 2>&1")) but no log file was created.
Any ideas?
Old 11-21-2012, 10:21 AM   #2
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
1. Your program may have a different PATH or other environment when run as CGI which prevents it from working.

2. Output from it may be visible in the apache error log.

3. Building commands to run in a shell ( system(...) ) using remote input from web users is dangerous and you'd be safer using Perl in taint mode and with system() in Perl's list format. If you must use C use execv() rather than system() so that input isn't given to a shell. More detail at

4. Is apache using a chroot so that /tmp/grep.log is in a place you didn't look and it might lack permissions for?
Old 11-21-2012, 10:27 AM   #3
Registered: Sep 2012
Posts: 48

Original Poster
Rep: Reputation: Disabled
Thanks for answering!
My program is not executing commands based on remote input from users, it just executes few system commands based on program logic.
The "commands" (grep and mutt) are in the PATH of the server, hovewer probably I need to check the chroot in apache configuration for the user...
I probably will try execv or popen/pclose.
Old 11-22-2012, 10:43 AM   #4
Registered: Sep 2012
Posts: 48

Original Poster
Rep: Reputation: Disabled
fork + execv solved the problem,


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Some Way to Execute a CGI Command Automatically des_a Programming 3 10-27-2007 07:18 PM
Execute a windows command from inside a c-cgi-script leamassiot General 51 02-09-2006 11:06 AM
Failed to execute cgi script in Apache!! baby_linu Linux - Newbie 4 06-30-2004 05:23 PM
Bash command $? failed to execute. Linh Programming 7 05-14-2004 12:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration