Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

system( 'mv ...' ) not working?

by ramses0 (Initiate)
on Sep 23, 2003 at 15:52 UTC ( [id://293544]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there. I've wrestled with this for days now, and haven't gotten anywhere. I'm using Perl to make web-based glue-code for an import (of log files) script. Summary of the problem is: "after a long running process system("import $file"), my system("mv $file /tmp/trash") isn't working" Here are more details...

Scenario is:

/var/log/newlogs/One_Log_Dir/log.txt, screenshots.jpg, etc.

So... put up a web-page with checkboxes and the output of:

@files = `find /var/log/newlogs -name log.txt -maxdepth 2`
(remember, this is glue-code, so I'm not using / learning readdir / opendir, etc yet).

...and after a submission (where $cgi->param('logs') is filled with checkboxed selected items...)

for my $log ( @logs ) { add_and_trash( $log ) }

Where add_and_trash() is something like:

sub add_and_trash { my ($name) = @_; my $newname = `convert $name`; print "<hr>\n"; my $cmd1 = "/tools/addbundle.pl $newname"; print $cmd1. "<hr>\n"; system( $cmd1 ); # can take ~2-3 minutes my $cmd2 = "mv $newname /tmp/trashed"; print $cmd2. "<hr>\n"; system( $cmd2 ); }

Can anyone see any problems? Every single function call is being called correctly. If I take the copy/paste of the print $cmd's and paste them manually into a bash session, everything works, it's only when run from the perl front-end that it bombs.

`whoami` reports back 'rames', not 'nobody'. Any ideas / assistance would be welcome. Sorry for chopping apart the script (it's long and boring, otherwise), hopefully my perl-fu is strong enough that I didn't make any syntax errors while paring it down.

I've done reading with perldoc perlops (for ``), and perldoc -f system, but just can't figure out why when *I* copy and paste the commands by hand it works, but perl (perl/cgi running through apache) can't get it working.

Assistance is appreciated, I'll respond with more details if there are parts that I'm missing.

--Robert

Replies are listed 'Best First'.
Re: system( 'mv ...' ) not working?
by snax (Hermit) on Sep 23, 2003 at 16:05 UTC
    It's not clear from your writeup what is happening when the second system call "fails". Does it actually have an error? Does it hang/timeout?

    Two suggestions, to start. Both are listed in the docs for system, too. First, use the "list" form (just a good habit) and second check return values/error values -- printing these out will help you debug, too.

    So: go readeth thee the docs at system again -- you say you do this, but no evidence of this exists in your code, explain more fully what the problem is (how it manifests), and let us know more about what's really going on :)

Re: system( 'mv ...' ) not working?
by bm (Hermit) on Sep 23, 2003 at 16:05 UTC
    Perhaps the user that runs the CGI, ie probably nobody, does not have permission to overwrite an existing /tmp/trashed. Check to see if the file already exists from the command line, and if so remove it.

    Alternatively, try this:

    use File::Copy; ... move($newname, '/tmp/trashed') or die "Failed to move file: $!\n";
    Lastly, perhaps a better question is why can't you just unlink $newname??
    --
    bm
Re: system( 'mv ...' ) not working?
by helgi (Hermit) on Sep 23, 2003 at 16:20 UTC
    Replace the system ($cmd) bits with

    system ($cmd1) == 0 or die "Cannot $cmd1:$?\n";

    and

    system ($cmd2) == 0 or die "Cannot $cmd2:$?\n";

    As you well know Bob, the error from child processes is contained in the $? variable, also known as $CHILD_ERROR.


    --
    Regards,
    Helgi Briem
    hbriem AT simnet DOT is

      re: the comment about the CGI user not having permissions

      I ran into this problem executing a move on a file that was uploaded via CGI. I was running apache and the file was being moved by user Apache. So I put Apache in my group and gave 775 permission to the directory. I don't know how good of a thing that is to do, but it did fix my problem.

      Additionally, when this happened to me, the system call didn't gen any error,but of course I didn't have fatals to Browser turned on. Have it upload to a 777 directory and see who owns the file, ala ls -l.

      This will teach me to look in the error logs... I tried the system() == 0 or die, and that helped... looks like the move target is not getting properly passed to the system call (ooh! or there is an extra embedded newline that the browser strips out of the "mv" command when copying!!!). I'll try to fix that. What's the "carp to browser" stanza? I'll try to look that up as well so it'll be easier to spot these next time. Thanks for the confessional debugging! --Robert
      mv: missing file argument Try `mv --help' for more information. sh: /var/log/newlogs/trashed: is a directory Uncaught exception from user code: Cannot: mv /var/log/qarun-newlogs/Static-Air-10.16.79.90 /var/log/newlogs/trashed, 32256 main::add_and_trash('/var/log/newlogs/Air-10.16.79.90-0922_125 +6PM') called at logloader.pl line 42
Re: system( 'mv ...' ) not working?
by Abigail-II (Bishop) on Sep 23, 2003 at 16:12 UTC
    mv typically writes an error message if it fails. What's the error you're getting?

    Abigail

Re: system( 'mv ...' ) not working?
by Roger (Parson) on Sep 24, 2003 at 02:56 UTC
    Ah, your error is probably because the code my $newname = `convert $name`; returned "name\n", and you didn't chomp the $newname. Thus when you get to the line:
    system("mv $newname /tmp/trashed");
    you are actually telling the shell to:
    mv name /tmp/trashed
    That is, running mv with only one parameter, name. The cure? Just add chomp($newname); after my $newname = `convert $name`;

Log In?
Username:
Password:

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

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

    No recent polls found