Changing permissions on files

I recently inherited a system and wanted to be edit all of the website files. I changed the group to admin and added myself to the group. Then I wanted to change all of the directories so I could have access to them, and change all of the file permissions so the group had read-write access and others (including Apache) had only read access. The command to do that is:

sudo chmod -R u+rwX,go+rwX,o-w .

-R recursively change
u+rwX users to read and write, and if it is a directory, also execute
go+rwX same as users for group and others
o-w then remove the write permission for others
. start here

Log results of perl scripts

While you are debugging perl scripts, the default for print statements is to display on the console. Often you want to have more info than fits nicely on the console or you want to be able to search through the results. In that case you can redirect the print commands with a simple redirect. e.g.

./ > test_results.txt

Once the script is debugged and running as a cron job, you might still want to see that it has successfully completed or has generated errors. In that case, redirecting the STDOUT and STDERR to log files is what you want to do.

I created a directory /var/log/My_logs and chmodded it to 755 and chowned it to admin. Then I added two lines at the beginning of the file.

#! /usr/bin/perl

use strict;
use warnings;

open(STDOUT, '>>',  $0 . ".log") or die "Can't open log";
open(STDERR, '>>',  $0 . ".error.log") or die "Can't open error log";

A couple of notes. the >> appends the results to the current file. $0 is the name of the perl script that is running. So if I run, the log file is perl_test.l.log. Also note that that is the complete file path and file name. If you are running the script manually, you might want to create log files in the same directory as the script. But if the script is part of a cron job, you might be better off writing the log files to /var/log/. And you probably don’t want the entire script do die if the log file can’t be opened.

An alternative way to do the same thing is to split the file name into parts with the fileparse function in the File::Basename module.

#! /usr/bin/perl

use strict;
use warnings;

 my($filename, $directories, $suffix) = fileparse($path);

open(STDOUT, '>>', "/var/log/My_Project_logs/" . $filename. ".log");
open(STDERR, '>>', "/var/log/My_Project_logs/" . $filename. ".error.log");

If you run the script as you, but later want it to be run as a cron job, make sure you change the permissions of the log files so that they match the permissions of the owner of the script.

If you write out a bunch of stuff, make sure you clean out the log files from time to time. Otherwise, set up a log rotation in /etc/logrotate.d/.