Re: Cron to email notification of ftp file upload?

From: Ohmster (bigbigkitty_at_ohmster.com)
Date: 01/02/04


Date: Fri, 02 Jan 2004 05:59:53 GMT

Noi,

Hmmm, this thread is getting a bit large to use direct quotes, I will do this
manually, addressing each point in order of relevance, and answer inline for
clarity.

>Whoa.... may I be so bold as to make another suggestion? Stop using root
>as your login to Linux, most *nix users frown on using root logins.

Yeah, I really should stop doing that. I use "su -" because I like having the
root path to issue commands. It is a pain in the ass to try and use tcpdump
or traceroute and getting "command not found" messages and then trying to
remember the paths for these commands or using "locate |grep bin" to try and
find them.

> 1. Use user account ohmster to do most of your work not root
> 2. Add new or current account ohmster to the /etc/sudoers file
> using visudo or your favorite editor, visudo brings up the /etc/sudoers
> file in vi for editing. For now add to the bottom of /etc/sudoers :

># %users localhost=/sbin/shutdown -h now
>ohmster ALL=(ALL) ALL

Wow, this is a very cool thing. Did this and it seems to work.

> 3. You can group root to user ohmster
>$ usermod ohmster -G root

Did this as well.
 
> 4. Logout of root and login as ohmster and do all your work as ohmster
>use sudo command when you need to make root privileged changes, ie,
>sudo chmod go+rwx /home/ftp/incoming
>sudo shutdown -r now
>sudo rpm -Uvh *.rpm

Did this and it seems to work. Using sudo as a prefix for commands now, have
to issue my ohmster password for some commands to do it but it works quite
well. Still do not have the root path environment for this stuff though.
Bummer.

> 5. As ohmster do the crontab -e for ohmster
>I think you can

Able to do this now as long as I prefix it with sudo, if sudo prefix is not
used, I get to edit a blank file.

># the following runs ftpwatch 1:15am, 7:15am, 1:15pm, 7:15pm
>$ echo "15 01,07,13,19 * * * /home/ohmster/scripts/ftpwatch" >
>/tmp/initial.cron

This seemed to work as I did not get any error return from the command
prompt.

>$ crontab -f /tmp/initial.cron

This does not work at all, see output below:

[ohmster@ohmster scripts]$ sudo crontab -f /tmp/initial.cron
Password:
crontab: invalid option -- f
crontab: usage error: unrecognized option
usage: crontab [-u user] file
        crontab [-u user] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e (edit user's crontab)
        -l (list user's crontab)
        -r (delete user's crontab)
[ohmster@ohmster scripts]$

> But if you use Webmin to schedule then continue to use that.

I used webmin and that works fine, checked crontab to see what it did, see
output below:

[ohmster@ohmster scripts]$ sudo crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.4732 installed on Fri Jan 2 00:08:42 2004)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
0 0 * * * /usr/bin/webalizer
0 0 * * * /etc/webmin/webalizer/webalizer.pl /var/log/httpd/access_log
0 0 * * * /etc/webmin/webalizer/webalizer.pl /var/log/vsftpd.log
0 2 * * * /var/www/cgi-bin/awstats.pl -config=ohmster -update
10 2 * * * /var/www/cgi-bin/awstats.pl -config=mail -update
15 1,7,13,19 * * * /home/ohmster/scripts/ftpwatch
[ohmster@ohmster scripts]$

I did move ftpwatch out of cron.daily and used your suggested path.

>> How should the if statement be placed in this file?...

> as below
>
>> [root@ohmster cron.daily]# pwd
>> /etc/cron.daily
>> [root@ohmster cron.daily]
>
>$ cat ftpwatch
>
>#!/bin/sh
>find /var/ftp/incoming -mtime -1 -type f ! -name ".message" >
> /tmp/ftp.list
>
>echo "return code 1 =" $?
>if [ $? = 0 ]
> then
> mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list
>fi

>echo "return code 2 =" $?

>> Pls post heads up if either works.
>I ask because I'm not 100% on what the return code will be with '! -name
>".message" '

Did this and tested it. First the file so you can see the modification:

[ohmster@ohmster scripts]$ cat ftpwatch
#!/bin/sh
find /var/ftp/incoming -mtime -1 -type f ! -name ".message" > /tmp/ftp.list
&& mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list

echo "return code 1 =" $?
if [ $? = 0 ]
 then
 mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list
fi

echo "return code 2 =" $?
[ohmster@ohmster scripts]$

Now we test the file:

[ohmster@ohmster scripts]$ sudo ./ftpwatch
Null message body; hope that's ok
return code 1 = 0
Null message body; hope that's ok
return code 2 = 0
[ohmster@ohmster scripts]$

Damn, still not working with an empty /var/ftp/incoming directory. This is
actually worse now because I get two empty body emails. Putting a file into
/var/ftp/incoming and testing again:

[ohmster@ohmster scripts]$ sudo ./ftpwatch
return code 1 = 0
return code 2 = 0
[ohmster@ohmster scripts]$

Got two emails informing me that there is a file in /var/ftp/incoming with
path and filename of the file.

Alright, now let's go over this new ftpwatch and try to figure out what is
wrong with it. I will analyze it the best I can, feel free to jump in with
corrections, please:

[ohmster@ohmster scripts]$ cat ftpwatch
#!/bin/sh
find /var/ftp/incoming -mtime -1 -type f ! -name ".message" > /tmp/ftp.list
&& mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list

echo "return code 1 =" $?
if [ $? = 0 ]
 then
 mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list
fi

echo "return code 2 =" $?

+++ find /var/ftp/incoming -mtime -1 -type f ! -name ".message" >
/tmp/ftp.list

This part is good, it finds new files in /var/ftp/incoming, pays no attention
to a file called ".message", and writes the results of the find command to
/tmp/ftp.list.

+++ && mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list

This part will mail /tmp/ftp/list to me regardless of any condition. "It just
goes".

+++ echo "return code 1 =" $?
if [ $? = 0 ]

Return code of what? This simply says echo the return code of $?. What is $?
anyway? We gave it no intitial value in the file as far as I can tell.

+++ then
 mail -s "FTP Updates" ohmster@localhost </tmp/ftp.list
fi

This mails /tmp/ftp.list regardless because I think that no parameters were
set for return code 1.

+++ echo "return code 2 =" $?

I have no clue as to what this is supposed to do.

This "if" statement seems like the right approach but I think it has to be
nested in the ftpwatch line, before the actual email command is given,
otherwise the email command will commence before it hits the if statement. In
other words, satisfy the if statment before executing the email command. If
the if statement is not satisfied, then do not process the mail command.

I really appreciate your help and your very good advice, Noi. I learned a lot
today. I hope that you will stick with me through this and that we can iron
out the no mail if directory is empty situation. As much for the practical
results of the newly created file as for the wonderful education that this
example is giving me.

Thanks again buddy!

-- 
~Ohmster


Relevant Pages

  • Re: Need a reliable modem hangup
    ... but circumvents those restrictions based ... > on a user and command definition. ... I looked into the sudo and /etc/sudoers man pages and it appears ... suid-bit not working on scripts. ...
    (comp.os.linux.networking)
  • [opensuse] tired of typing: find . -type d -print0 | xargs -0 chmod 0755 - heres a s
    ... I got tired of having to continually type the long find command to set ... Help yourself to the scripts if you have a need at: ... Called with no argument the scripts set default permissions for files at 0644 ... it currently calls find with sudo, so if you have not configured sudo ...
    (SuSE)
  • Re: Help - Restrict root privileges - What is indispensable?
    ... Can access to my frontend netfilter shorewall. ... Use chmod, run sh scripts, and any command essential for running the OS. ... privileges" and not simply restrict access via sudo, then you should be looking at SElinux. ...
    (Ubuntu)
  • Re: [kde] su identification
    ... assumes that you wish to invoke the root account and will demand Root ... A user may ONLY sudo as allowed in the /etc/sudoers ... allowing a command with any parameters ... This config allows my normal user to do whatever he'd normally be able to ...
    (KDE)
  • Re: vi horizontal split screen
    ... efficient at processing scripts, and one way to do that was substitute ... Microsoft's work for OLE in Windows 1x through 3x. ... As more and more users come to Linux who have no desire for command line ... effectively a programming environment. ...
    (comp.editors)