You are not logged in.

#1 2009-06-24 17:24:52

rusty99
Member
Registered: 2009-03-18
Posts: 253

[Solved] Script problem

I'm trying to have a script run once every hour to check my inbox for new mail and write the output in wmii's bar.
It works fine if I restart crond after logging in, however if crond isn't restarted it wont create the notification.

The script is in .bin/ , other cronjobs run with no issue. I've tried using full paths in the script but it seems to make no difference, where am I going wrong?

 > cat .bin/mailcount 
#!/bin/bash

offlineimap &
sleep 5
DIR=$HOME/.GMail/INBOX/new/
NEW=`find $DIR -type f | wc -l`

echo -n Mail: $NEW | wmiir create /rbar/mail
sleep 120
wmiir remove /rbar/mail

exit

Last edited by rusty99 (2009-06-26 20:53:52)

Offline

#2 2009-06-24 18:39:55

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [Solved] Script problem

Does the call to "wmiir create" occur before wmii is started?

Offline

#3 2009-06-24 21:36:02

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

No it isn't. I'm thinking the easiest work around at present is to drop the script in .wmii-hg/ and run it as a menu entry or to put an entry in rc.wmii.local that does what I want.
It's just odd that the script will work correctly but only after a crond restart.

Offline

#4 2009-06-24 22:53:07

cschep
Member
Registered: 2006-12-02
Posts: 124
Website

Re: [Solved] Script problem

does it work once per crond restart? or does it continue working after restarted?

maybe it's being restarted as a different user?

not sure just shooting from the hip here.

Offline

#5 2009-06-24 23:10:18

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

Aye, works fine after the initial crond restart, every hour it'll pop up the notification.
If I reboot then I have to do a crond restart after login to get it working again. Certainly a weird one, backup scripts run fine from the same path every day with no issue.
I'm assuming it's the 'wmiir create /rbar/mail' thats causing the problem, although I can't see why hmm.

Offline

#6 2009-06-25 01:05:31

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [Solved] Script problem

Try putting a sleep 5 or sleep 10 or sleep 30 at the start of the script... If it starts working then without needing to restart cron, then you can be pretty confident it's a timing issue with startup...

Offline

#7 2009-06-25 08:44:02

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

Still no luck. Tried preceding everything with sleep 30 but it acts just as before.
I think I'll have to be content with the script as a menu item, atleast I can fire it whenever I choose then.

Offline

#8 2009-06-25 09:01:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Script problem

PATH and HOME are probably wrong. You could try wrapping the entire script in a while true loop.

Offline

#9 2009-06-25 10:29:45

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

I've tried using full paths within the script which didn't seem to have any effect, also tried placing it in .wmii-hg/ as that gets added to PATH at startup.
Despite this not working as originally intended I think I'll mark it as solved?, I'm finding that running it as a menu item is no great shakes.
Now If I could just work out how to make 'urxvt -e mutt' open in 'mail' tag...

Offline

#10 2009-06-25 14:15:02

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [Solved] Script problem

rusty99 wrote:

Now If I could just work out how to make 'urxvt -e mutt' open in 'mail' tag...

Simple enough, give urxvt -name "mutt" and match on /mutt:URxvt:mutt/ in wmiirc.

Offline

#11 2009-06-25 17:10:42

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

Nice one, thanks for the pointer.
I ended up using -title as I couldn't get rid of the damn scrollbar smile.

Offline

#12 2009-06-26 16:56:04

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

This is what I came up with but as it's my first real attempt at any scripting I was hoping someone would be kind enough to see if there's a cleaner way of doing it.
The main issue was seeing if there was a running instance of mutt and getting a value for it, which I got from google searching, but what does $? actually achieve?

#!/bin/bash

offlineimap &> /dev/null 
sleep 2
DIR=/home/rusty/.GMail/INBOX/new/
NEW=`find $DIR -type f | wc -l`
ps -C mutt &> /dev/null

if [ $? -ne 0 ] && [ $NEW -gt 0 ]; then
    echo -n Mail: $NEW | wmiir create /rbar/mail
    urxvt -title mutt -e mutt &
    sleep 15
    wmiir remove /rbar/mail
else
    if [ $? -gt 0 ] && [ $NEW -ge 1 ]; then
        echo -n Mail: $NEW | wmiir create /rbar/mail
        sleep 15
        wmiir remove /rbar/mail
    else
        if [ $? -gt 0 ] && [ $NEW -eq 0 ]; then
            echo -n Mail: $NEW | wmiir create /rbar/mail
            sleep 10
            wmiir remove /rbar/mail
        fi
    fi
fi
exit

Offline

#13 2009-06-26 17:12:51

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Script problem

if i understand your goal right, i would do it this way. 

$? is the exit status of the previous ps command: 0 for success, i.e. mutt is running; else means failure, i.e. mutt is not running.  in my version i do the same thing by doing `pgrep mutt ||` which only runs the part after the || if the part before the || fails (same as $? != 0)

DIR=/home/rusty/.GMail/INBOX/new/
NEW=`find $DIR -type f | wc -l`

if [ $NEW -gt 0 ]; then
   echo -n Mail: $NEW | wmiir create /rbar/mail
   pgrep mutt || urxvt -title mutt -e mutt &
   sleep 15
   wmiir remove /rbar/mail
else
   echo -n Mail: $NEW | wmiir create /rbar/mail
   sleep 10
   wmiir remove /rbar/mail
fi

this will show new message count, run mutt (if not running already), sleep 15 if you have new mails and it will show new message count (0) and sleep 10 if you don't.

/edit:

i may have misunderstood you goal.  but i think it's because your script doesn't make much sense.  you've got a few nested double ifs that are either contradictory or redundant. 

[ $? -ne 0 ] and [ $? -gt 0 ] will always return the same true or false value as each other (commands don't have negative exit codes).

[ $NEW -gt 0 ] and [ $NEW -ge 1 ] will also always return the same true or false value as each other (you can't have a negative number of files in $DIR).

also your script does nothing if mutt is in fact running.  is this intended? it would make things simpler.

Last edited by brisbin33 (2009-06-26 17:46:56)

Offline

#14 2009-06-26 19:48:03

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: [Solved] Script problem

Gotcha, your ammendment does exactly what's intended, thanks brisbin
I feel kinda daft about the $? now that you clarify it, exit codes is one part I glossed right over when reading up..

Regarding [ $NEW -gt 0 ] and [ $NEW -ge 1 ]
It's funny how some quite obvious mistakes only come to light once they're pointed out.

brisbin33 wrote:

also your script does nothing if mutt is in fact running.  is this intended? it would make things simpler.

I noticed on occasion that if I recieved 4 mail it would still show 4 new mail in the notification bar despite having read them, in which case I would have to quit mutt and restart it, but the majority of the time it did seem to show the correct number of new mails even if mutt was running, sounds like a fluke smile

The idea was mutt is run on a seperate workspace, the script is in .wmii-hg/ so can be run from Mod1+a regardless of which workspace I'm on and inform me of mail status, so to answer your question, nope it's meant to grab number of mail or (0) even if mutt is already running.

Thanks again, much appreciated.

Offline

#15 2009-06-26 20:04:27

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Script problem

rusty99 wrote:

I noticed on occasion that if I recieved 4 mail it would still show 4 new mail in the notification bar despite having read them, in which case I would have to quit mutt and restart it, but the majority of the time it did seem to show the correct number of new mails.

if you make changes to your mailbox within mutt (i.e. read, or delete messages), those changes are not reflected in the file system until you sync.  by default mutt only syncs if you a) change mailboxes b) exit mutt or c) press $

hopefully this is why you're getting odd message counts, it's just not synced yet.

Offline

Board footer

Powered by FluxBB