C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-12-2009, 11:29 AM   #1
Registered User
 
Join Date: May 2007
Location: Berkeley, CA
Posts: 140
Calling lockf from the shell line works fine, but in the script, it fails

Everything seems to work fine when I do the following...

Code:
% lockf -t 30 /usr/party/log/party.log sleep 30
Now when I add it to a perl script like in the following

Code:
#!/usr/bin/perl
use warnings;

my $value;
while (True)
{
    if(`w | grep cd | grep party`) {
        `lockf -t 30 /usr/party/log/party.log sleep 30`;
        sleep(30);
    }
    else {
        print "not logged in\n";
    }
    sleep(1);

}
It doesn't work even though lockf shows up on my ps table.

Quote:
cdalten 50123 0.0 0.2 3764 2580 pl IN 12:51PM 0:00.32 -zsh (zsh)
cdalten 52997 0.0 0.2 2796 2276 pl SN+ 1:30PM 0:00.05 /usr/bin/perl ./scan.pl (perl5.8.8)
cdalten 53078 0.0 0.1 1256 596 pl SN+ 1:31PM 0:00.00 lockf -t 30 /usr/party/log/party.log sleep 30
cdalten 53079 0.0 0.0 1256 432 pl SN+ 1:31PM 0:00.00 sleep 30
I don't get why it fails when I run it in a perl script.
Overworked_PhD is offline   Reply With Quote
Old 04-12-2009, 11:50 AM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,207
It seems silly to me to use a perl script just to issue shell commands; why don't you put them in a shell script?

To lock a file in perl use flock().
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Old 04-12-2009, 11:58 AM   #3
Registered User
 
Join Date: May 2007
Location: Berkeley, CA
Posts: 140
Quote:
Originally Posted by MK27 View Post
It seems silly to me to use a perl script just to issue shell commands; why don't you put them in a shell script?

To lock a file in perl use flock().
I'd still get the same results and hence it wouldn't solve my current problem.
Overworked_PhD is offline   Reply With Quote
Old 04-12-2009, 01:02 PM   #4
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,207
Quote:
Originally Posted by Overworked_PhD View Post
I'd still get the same results and hence it wouldn't solve my current problem.
are you saying that this
Code:
#!/bin/bash

lockf -t 30 /usr/party/log/party.log sleep 30
will not execute EXACTLY as if you had typed it into the command-line? Because I don't believe you!

I do not have a "lockf" command on my system, so I can't help you with the syntax*, but shell scripts are executed by the shell one line at a time. If it works on the command line, it will work in a shell script. Here:
Code:
!/bin/bash

if grep "bash" | grep "cdalten" > /dev/null
    then lockf -t 30 /usr/party/log/party.log sleep 30
    else echo "not logged in"
fi
The purpose of "> /dev/null" is to spare you the grep output on screen.

*eg, I'm assuming "sleep 30" at the end is a parameter and not a typo for "&& sleep 30"
__________________

"A man can't just sit around." -- Larry Walters

Last edited by MK27; 04-12-2009 at 01:41 PM.
MK27 is online now   Reply With Quote
Old 04-12-2009, 03:28 PM   #5
Registered User
 
Join Date: May 2007
Location: Berkeley, CA
Posts: 140
Your script only works after I press ctrl-c. I'm assuming the process is doing some kind of forking that I'm not aware of.
Overworked_PhD is offline   Reply With Quote
Old 04-13-2009, 09:55 AM   #6
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,207
Quote:
Originally Posted by Overworked_PhD View Post
Your script only works after I press ctrl-c. I'm assuming the process is doing some kind of forking that I'm not aware of.
Code:
cat textfile | thatscript.sh
or
thatscript.sh < textfile
Sorry, it was intended to process input, I thought that was clear.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
This works fine with IP 127.0.0.1 but fails with my REAL IP. azjherben Networking/Device Communication 15 05-19-2009 10:28 PM
shell script basic question PedroTuga Linux Programming 1 09-09-2006 03:24 AM
It works just fine but jcmhex C++ Programming 3 04-24-2005 06:53 PM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM
Greenhand want help! leereg C Programming 6 01-29-2002 06:04 AM


All times are GMT -6. The time now is 09:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22