# Can't execute shell commands in CGI

**URL:** <https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822>\
**Category:** Ruby\
**Created:** [August 15, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822 "2009-08-15T23:39:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 15, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/1 "2009-08-15T23:39:38Z")

</div>

It is necessary to execute linux command in CGI(apache)

`def cmd(com)
 system("#{com}>log.log")
end
cmd("pwd")`  
There isn’t errors but log.log is empty  
Else:

`def cmd
 system("pwd>log.log")
end
cmd`  
there isn’t result too.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 15, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/2 "2009-08-15T23:40:45Z")

</div>

Luckystr Luckystr wrote:

> It is necessary to execute linux command in CGI(apache):

def cmd(com)

> system("#{com}\>log.log")  
> end  
> cmd(“pwd”)

> There isn’t errors but log.log is empty  
> Else:

> def cmd  
> system(“pwd\>log.log”)  
> end  
> cmd

> there isn’t result too.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 17, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/3 "2009-08-17T22:46:52Z")

</div>

7stud – wrote:

> Luckystr Luckystr wrote:
> 
> > It is necessary to execute linux command in CGI(apache)
> > 
> > `def cmd(com)
> > system("#{com}>log.log")
> > end
> > cmd("pwd")`  
> > There isn’t errors but log.log is empty  
> > Else:
> > 
> > `def cmd
> > system("pwd>log.log")
> > end
> > cmd`  
> > there isn’t result too.
> 
> 1. There aren’t any code tags here. You can just post your code and the  
> indenting will be preserved.
> 
> 2. Where is log.log located in your Apache directory structure? The  
> correct path to that file needs to be specified in your cgi script.
> 
> 3. Check your apache error logs located here:
> 
> /logs/error\_log
> 
> and see what the last few lines say. It may say something like:
> 
> log.log: Permission denied
> 
> In that case, you’ll need to change the permissions for log.log to give  
> ‘others’ write privileges, so that your script can write to log.log,  
> something like this:
> 
> $ sudo chmod o+w /full/path/to/log.log

errorlog:  
sh: log.log: Permission denied

The path log.log can be correct since “\>” creates a file if it does not  
exist.  
But just in case, I checked path in script make access pattern 0777.  
Outgoing from logfile, I think that there is error because user “apache”  
doesn’t have shell.  
I tried to make apache’s shell by “adduser” by “root” but linux said  
that it’s impossible.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 18, 2009, 4:42am UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/4 "2009-08-18T04:42:42Z")

</div>

Hi,

Am Dienstag, 18. Aug 2009, 05:46:54 +0900 schrieb Luckystr Luckystr:

> errorlog:  
> sh: log.log: Permission denied

Redirect $stderrr, too. Direct it into /tmp to avoid access rights  
problems while you’re testing.

Bertram

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 16, 2009, 4:58am UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/5 "2009-08-16T04:58:13Z")

</div>

Luckystr Luckystr wrote:

> It is necessary to execute linux command in CGI(apache)
> 
> `def cmd(com)
> system("#{com}>log.log")
> end
> cmd("pwd")`  
> There isn’t errors but log.log is empty  
> Else:
> 
> `def cmd
> system("pwd>log.log")
> end
> cmd`  
> there isn’t result too.

1. There aren’t any code tags here. You can just post your code and the  
indenting will be preserved.

2. Where is log.log located in your Apache directory structure? The  
correct path to that file needs to be specified in your cgi script.

3. Check your apache error logs located here:

/logs/error\_log

and see what the last few lines say. It may say something like:

log.log: Permission denied

In that case, you’ll need to change the permissions for log.log to give  
‘others’ write privileges, so that your script can write to log.log,  
something like this:

$ sudo chmod o+w /full/path/to/log.log

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 18, 2009, 12:33pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/6 "2009-08-18T12:33:58Z")

</div>

Luckystr Luckystr wrote:

> It is necessary to execute linux command in CGI(apache)
> 
> `def cmd(com)
> system("#{com}>log.log")
> end
> cmd("pwd")`  
> There isn’t errors but log.log is empty

Try:  
system(“pwd \>/tmp/log.log”)

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [August 18, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/cant-execute-shell-commands-in-cgi/173822/7 "2009-08-18T22:39:52Z")

</div>

Brian C. wrote:

> Luckystr Luckystr wrote:
> 
> > It is necessary to execute linux command in CGI(apache)
> > 
> > `def cmd(com)
> > system("#{com}>log.log")
> > end
> > cmd("pwd")`  
> > There isn’t errors but log.log is empty
> 
> Try:  
> system(“pwd \>/tmp/log.log”)

thank all, and thank you, Brian C… /tmp/log.log has pwd
