Hello to all Monks,

I have created a webpage using CGI (say page.pl) and writing logs in a log file (say file.log) that already exists. Both the perl program and logfile are under same directory (say dir1) owned by application user (say app1). The directory dir1 has 755 permission.

Now I am trying to modify my code so that if the file.log doesn't exist, it should be created and should have file permission to be writable by apache (as apache is configured to run the webpage).

Can you please suggest me a clean way to do this? I have tried something like:

my $logfile = "file.log"; unless(-e $logfile) {#Create the log file if it doesn't exist open my $fc, ">", $logfile or die "Could not create file '$logfile +' $!"; close $fc; } open(my $fh, ">>", $logfile) or die "Could not open file '$logfile' $! +";

But this won't work unless the dir1 has 777 permission as it's owned by app1 and not apache. I won't be making dir1 777 at all! I found another post (#585219) which shows I can use sysopen but even with this option, the file.log will be owned by root (neither apache nor app1) and I think this means the file.log will need to have 777 permission.

I am trying to find a way to create this file.log (if it doesn't exist) in such a way that I don't have to change access to file.log or dir1 to 777. At max I can make it 775

Thank you for taking time to read this!

UPDATE: Changed subject line to mark this solved.


In reply to [Solved]: How to create a log file under a directory owned by app user from cgi script? by Perl300

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.