Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

system command is not working

by gulla (Initiate)
on Dec 07, 2011 at 03:09 UTC ( [id://942150]=perlquestion: print w/replies, xml ) Need Help??

gulla has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have strange problem with the system command in my perl script. I have a perl script with system command in the file a.pl e.g: #!/usr/bin/perl system("ls -lrt > a.out"); If I execute the script directly, as ./a.pl, it displays the result. However, if invoke the script with "at" command, it doesnt turn up. I mean it wouldnt generate the output file. I executed the command like this #at -f /tmp/a.pl now Please let me know where it could go wrong.. Thanks In Advance, Thanks, Gulla

Replies are listed 'Best First'.
Re: system command is not working
by Anonymous Monk on Dec 07, 2011 at 03:31 UTC
Re: system command is not working
by ikegami (Patriarch) on Dec 07, 2011 at 04:23 UTC

    So you're having an error. First thing you should do is check what error happened.

    • What error does ls the command output to STDERR?
    • What error does system report?

    Likely cause: The current directory isn't what you think it is, and you don't have access to write to the current directory.

Re: system command is not working
by vinian (Beadle) on Dec 07, 2011 at 05:55 UTC
    check the current working directory when execute the command, it's possiblely in the user's home directory who execute the command . or try this
    #!/usr/bin/perl # a.pl use strict; use warnings; system("ls -lrt > /tmp/a.out");
      Hi, Thanks for the reply. Definitely works whatever you have suggested. However, if I execute a shell script with "at" command like below #b.sh at -f a.pl now it doesnt work. The system command in the perl script is not recognized. Can you please try your end.. Thanks, Gulla
        where is the time specification for your "at" comand?. It seems that your script waits forever
Re: system command is not working
by jffry (Hermit) on Dec 07, 2011 at 22:32 UTC

    I agree with pvaldes, it seems like you are not using at correctly. In fact, I'm surprised at is not returning an error immediately. I have tried three Unix flavors and get an error when I do not specify a time.

    me@myaixbox:/home/me/sandbox $ at -f 1.pl at: 0481-110 Command usage error; a parameter is missing. me@myaixbox:/home/me/sandbox $ uname AIX
    me@mylinuxbox:~/sandbox $ at -f 1.pl Garbled time me@mylinuxbox:~/sandbox $ uname Linux
    userid@somebox:/tmp/tmp.66 # at -f 1.pl bad time specification userid@somebox:/tmp/tmp.66 # uname HP-UX
      Hello Friends, Thanks for your replies. In fact , the "at" command which I place in my reply earlier inlucdes the "now" at the end. I am sorry that it got merged with the sentense. The actual command in the script is "at -f a.pl now" Thanks, Gulla
Re: system command is not working
by jffry (Hermit) on Dec 08, 2011 at 15:13 UTC

    I can recreate your problem now.

    me@mybox:/home/me/sandbox $ cat 1.pl #!/usr/bin/perl system('echo bob >/tmp/blarney.out'); me@mybox:/home/me/sandbox $ ./1.pl me@mybox:/home/me/sandbox $ cat /tmp/blarney.out bob me@mybox:/home/me/sandbox $ rm /tmp/blarney.out me@mybox:/home/me/sandbox $ at -f 1.pl now Job me.1323356661.a will be run at Thu Dec 8 10:04:21 EST 2011. me@mybox:/home/me/sandbox $ at -l me@mybox:/home/me/sandbox $ ls -l /tmp/blarney.out ls: 0653-341 The file /tmp/blarney.out does not exist.

    When I check my mail, I see the problem. Notice the "command not found" message. This means you must supply full path to the script.

    me@mybox:/home/me/sandbox $ mail Mail [5.2 UCB] [AIX 5.X] Type ? for help. "/var/spool/mail/me": 2 messages 1 new 2 unread U 1 daemon Thu Dec 8 10:02 32/950 "Output from at job +1.pl, me" >N 2 daemon Thu Dec 8 10:04 31/940 "Output from at job +1.pl, me" ? 1 Message 1: From daemon Thu Dec 8 10:02:08 2011 Date: Thu, 8 Dec 2011 10:02:08 -0500 From: daemon To: me Subject: Output from at job 1.pl, me@mybox, exit status 127 Cron Environment: SHELL = /opt/freeware/bin/bash PATH=/usr/bin:/etc:/usr/sbin:/usr/bin/X11:/sbin:/usr/java14/jre/bin:/ +usr/java14/bin:/usr/local/bin CRONDIR=/var/spool/cron/crontabs ATDIR=/var/spool/cron/atjobs LOGNAME=me HOME=/home/me Your "at" job executed on mybox on Thu Dec 8 10:02:08 EST 2011 1.pl produced the following output: bash: line 3: 1.pl: command not found ***************************************************************** cron: The previous message is the standard output and standard error of one of the cron commands.

    Now with full path supplied.

    me@mybox:/home/me/sandbox $ ls -l /tmp/blarney.out ls: 0653-341 The file /tmp/blarney.out does not exist. me@mybox:/home/me/sandbox $ at -f /home/me/sandbox/1.pl now Job me.1323356887.a will be run at Thu Dec 8 10:08:07 EST 2011. me@mybox:/home/me/sandbox $ at -l me@mybox:/home/me/sandbox $ ls -l /tmp/blarney.out -rw-r----- 1 me users 4 Dec 08 10:08 /tmp/blarney.out

      Kudos for running it all down, aren't logs magical?

      :D

      Thanks for the detailed analysis and step by step walk through.

      I tried the way you have advised. However, this time my error file says that "/sbin/sh: system: not found". Below is the code I tried,

      -------- #a.pl #!/usr/bin/perl system('ls -lrt > a.out'); -------- Comand $ at -f /tmp/a.pl now commands will be executed using /sbin/sh job 1323397408.a at Fri Dec 9 13:23:28 2011 Error: /sbin/sh: system: not found

      I am wondering why the external system command is not recognized in the perl.

      Any thoughts my friend. When the perl script is executed with "At" command, it invokes a shell to execute the script. However, that shell is unable to recoginse the system command. Is that correct??

      Please let me know what else I can try to resolve this issue.

      Thanks In Advance

        I am wondering why the external system command is not recognized in the perl.
        The error is reported by /bin/sh, not perl. For some reason, the shell tries to interpret the script. Is the #!/usr/bin/perl line really the first one in your script?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://942150]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-18 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found