Urban Terror Forums: Help with making shell script - Urban Terror Forums

Jump to content

 Login | Register 
Advertisement
  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

Help with making shell script Rate Topic: -----

#1 User is offline   Aarsbuds0r (old) Icon

  • Joined: 21-January 04
  • Posts: 1,334
  • LocationNetherlands, The

Posted 02 June 2004 - 02:37 AM

I am wondering if anyone can help me out. Linux.

I would like to run once a night (at 6am) a script, that will copy all pb-screenies that have been made the past 24hours to a folder with yesterdays date as a name. Folders that are older then 4 days should be removed.

I think this can be done with a fairly simple shellscript, but I dont know anything about it. I could off course try to find out everything myself, but maybe someone hear can do it in 5 minutes and is willing to help me.

It would be something like:

for all files in home/games/quake3/pb/svss
if extension = png and creationdate is younger then 24 hours
copy to home/games/quake3/pb/svarchives/$todaysdate/

for all folders in home/games/quake3/pb/svarchives
if filename.date is older then 4 days, delete folder.

#2 User is offline   illogical Icon

  •   verified user   
    Retired Master Server Administrator
  • Account: illogical
  • Main tag: 6th|
  • Country:
  • Joined: 08-March 10
  • Posts: 2,349

Posted 02 June 2004 - 12:07 AM

I'm cooking you up something.

#3 User is offline   illogical Icon

  •   verified user   
    Retired Master Server Administrator
  • Account: illogical
  • Main tag: 6th|
  • Country:
  • Joined: 08-March 10
  • Posts: 2,349

Posted 02 June 2004 - 12:35 AM

Ok, this is tested code, so let me know of any failures. Also, it's Perl, so make sure the interpreter path is correct, right now it's /usr/bin/perl. Also, if you don't have it already for some reason, you need the File::stat module, so from a root shell do perl -MCPAN -e 'install File::stat' to be sure you have it.

First, here is the cron line to add. You can do this via the crontab -e command:
02 6 * * * /home/games/quake3/do_pb_svss


Then in the file /home/games/quake3/do_pb_svss add the following code:
#!/usr/bin/perl -w
# Copyright (C) 2004 Jeff Walter
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use File::stat;

my ($import_dir_a,$import_dir_b) = ('/home/games/quake3/public_html/screeny/','/home/games/quake3/public_html/screeny2/');
my ($export_dir) = ('/home/games/quake3/public_html/ssarchives/');

my ($sec,$min,$hour,$mday,$mon,$year,undef) = localtime(time - 86400); 
$year += 1900;
$mon++;

my ($date,$epoch)  = ($year . sprintf("%02d", $mon) . sprintf("%02d", $mday),time);

mkdir($export_dir . $date) || die "a horrible death";

open (OUT,">$export_dir$date/index.html");
print OUT "<html><head><title>$year/$mon/$mday</title></head><body>n";
print OUT "<table border="2"><tr><th>Screenshot</th><th>Nick</th><th>OS</th><th>GUID</th><th>Valid</th><th>Date/Time</th></tr>n";

my ($list,$file,$fs,@args,$colour,$link);
my ($pb_num,$pb_nick,$pb_os,$pb_guid,$pb_valid,$pb_datetime);

$list = `find $import_dir_a -name *.png`;
@list = split /n/, $list;
if (@list > 0) {
   foreach $file (@list) {
      $fs = stat($file);
      if ($epoch - (scalar $fs->mtime) < (24 * 60 * 60)) {
         @args = ('cp',$file,$export_dir . $date . '/');
         if (system(@args) != 0) {
            print "Copy of $file failedn";
            next;
         }
      }
   }
}
if (stat($import_dir_a . 'pbsvss.htm')) {
   open (IN,"<" . $import_dir_a . "pbsvss.htm");
   while (<IN>) {
      $_ =~ /_blank>(d+)</a> "(.+)" ((.+)) GUID=(.{32})((.+)) [(.+)]/i;
      ($pb_num,$pb_nick,$pb_os,$pb_guid,$pb_valid,$pb_datetime) = ($1,$2,$3,$4,$5,$6);
      if ($pb_valid =~ /valid/i) {
         $colour = "#00FF00";
      } elsif ($pb_valid =~ /unkn/i) {
         $colour = "#FFFF00";
      } else {
         $colour = "#FF0000";
      }
      if (stat("$export_dir$date/pb$pb_num.png")) {
         $link = "><a href="pb$pb_num.png">$pb_num</a>";
      } else {
         $link = " bgcolor="#FF0000">FAILED";
      }
      print OUT "<tr><td$link</td><td>$pb_nick</td><td>$pb_os</td><td bgcolor="$colour">$pb_guid</td><td bgcolor="$colour">$pb_valid</td><td>$pb_datetime</td></tr>n";
   }
   close (IN);
   @args = ('cp',$import_dir_a . 'pbsvss.htm',$export_dir . $date . '/pbsvss_a.htm');
   system(@args);
}



$list = `find $import_dir_b -name *.png`;
@list = split /n/, $list;
if (@list > 0) {
   foreach $file (@list) {
      $fs = stat($file);
      if ($epoch - (scalar $fs->mtime) < (24 * 60 * 60)) {
         @args = ('cp',$file,$export_dir . $date . '/');
         if (system(@args) != 0) {
            print "Copy of $file failedn";
            next;
         }
      }
   }
}
if (stat($import_dir_b . 'pbsvss.htm')) {
   open (IN,"<" . $import_dir_b . "pbsvss.htm");
   while (<IN>) {
      $_ =~ /_blank>(d+)</a> "(.+)" ((.+)) GUID=(.{32})((.+)) [(.+)]/i;
      ($pb_num,$pb_nick,$pb_os,$pb_guid,$pb_valid,$pb_datetime) = ($1,$2,$3,$4,$5,$6);
      if ($pb_valid =~ /valid/i) {
         $colour = "#00FF00";
      } elsif ($pb_valid =~ /unkn/i) {
         $colour = "#FFFF00";
      } else {
         $colour = "#FF0000";
      }
      if (stat("$export_dir$date/pb$pb_num.png")) {
         $link = "><a href="pb$pb_num.png">$pb_num</a>";
      } else {
         $link = " bgcolor="#FF0000">FAILED";
      }
      print OUT "<tr><td$link</td><td>$pb_nick</td><td>$pb_os</td><td bgcolor="$colour">$pb_guid</td><td bgcolor="$colour">$pb_valid</td><td>$pb_datetime</td></tr>n";
   }
   close (IN);
   @args = ('cp',$import_dir_b . 'pbsvss.htm',$export_dir . $date . '/pbsvss_b.htm');
   system(@args);
}

print OUT "</table><br /><center>written by <a href="mailto:jeff@404ster.com"><b>Jeff Walter</b></a> for <b>University Twente</b></center></body></html>";
close (OUT);

$list = `ls $export_dir`;
@list = split /n/, $list;
if (@list > 0) {
   foreach $file (@list) {
      $fs = stat($export_dir . '/' . $file);
      if ($epoch - (scalar $fs->mtime) > (5 * 24 * 60 * 60)) {
         @args = ('rm','-rf',$export_dir . '/' . $file);
         if (system(@args) != 0) {
            print "Removal of $export_dir/$file failedn";
            next;
         }
      }
   }
}
exit 0;


chmod the file to atleast 700, then run by hand atleast once to be sure it runs. If you have any questions, just let me know.

#4 User is offline   illogical Icon

  •   verified user   
    Retired Master Server Administrator
  • Account: illogical
  • Main tag: 6th|
  • Country:
  • Joined: 08-March 10
  • Posts: 2,349

Posted 02 June 2004 - 12:44 AM

Oh yeah, if it fails, all the notices will be sent to the mailbox of the user the script runs as.

#5 User is offline   Aarsbuds0r (old) Icon

  • Joined: 21-January 04
  • Posts: 1,334
  • LocationNetherlands, The

Posted 02 June 2004 - 02:26 PM

First of all: THANKS :lol:

2nd: In /usr/bin there's a file called "perl". So thats fine I gues.
3rd: In the same dir theres also a file called "stat". So I gues thats fine too?
4th: That email-thingy, will it send it to the MAILTO= thats in the crontab? If not, where can I set the emailaddress for the user?

bullet_loaderAdvertisement

#6 User is offline   Aarsbuds0r (old) Icon

  • Joined: 21-January 04
  • Posts: 1,334
  • LocationNetherlands, The

Posted 02 June 2004 - 02:51 PM

Some other questions: I just thought of the fact that I have two (/home/games/quake3/pb/svss/ and /home/games/quake3/pb/svss2/) different dirs where pb-screenies are (but they should both be copied to the same dir). What do I need to change for that? I have set that the filenames of the screenys will be 1-500 in svss and 501-1000 in svss2, so there wont be any problems with filenames.

Also, I think the folders that are made in your script are named as todays date, instead of yesterdays. Or does 'chomp' make it yesterday?

And I gues its no problem if I change the dirs in the file? If I would change "/home/games/quake3/pb/svss/" to "/home/games/quake3/.q3a/pb/svss/" its all still working, right?

#7 User is offline   Aarsbuds0r (old) Icon

  • Joined: 21-January 04
  • Posts: 1,334
  • LocationNetherlands, The

Posted 02 June 2004 - 03:02 PM

Okay, I just thought of something else too.

In the svss folders, there will be a file called "pbsvss.htm". I would like that in the archives as well. But this file has the same name for both folders, so there has to be little renaming. From the first dir "pbsvss1.htm" and from the 2nd "pbsvss2.htm" eg.

I realy hope you have time and feel like adding these new things I came up with.

#8 User is offline   illogical Icon

  •   verified user   
    Retired Master Server Administrator
  • Account: illogical
  • Main tag: 6th|
  • Country:
  • Joined: 08-March 10
  • Posts: 2,349

Posted 02 June 2004 - 10:56 AM

The `stat` program is not the same, but you should have the File::stat package, so should be safe.

The emails... You run the script as a user on the server. Every user has a system mailbox. The mail goes there, no way to change that as it is done by the cron daemon.

For copying from multiple directories, I'll have to make a code addition.

And yes, my bad, it does use the current date, not the previous day. Guess I'll fix that.

Changing the directories shouldn't be a problem, so long as they exist ;-)

#9 User is offline   illogical Icon

  •   verified user   
    Retired Master Server Administrator
  • Account: illogical
  • Main tag: 6th|
  • Country:
  • Joined: 08-March 10
  • Posts: 2,349

Posted 02 June 2004 - 11:05 AM

Ok, code updated. Look at the original code post for the new version.

#10 User is offline   Aarsbuds0r (old) Icon

  • Joined: 21-January 04
  • Posts: 1,334
  • LocationNetherlands, The

Posted 02 June 2004 - 06:21 PM

Oh thanks so much man, you did realy nice job =]]

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users

Sponsored link
https://www.urbanterror.info/members/donate/


Copyright © 1999-2024 Frozensand Games Limited  |  All rights reserved  |  Urban Terror™ and FrozenSand™ are trademarks of Frozensand Games Limited

Frozensand Games is a Limited company registered in England and Wales. Company Reg No: 10343942