Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 6 of 6

This is a discussion on 50% CPU usage when using system function in the VPS & Dedicated forum
I'm attempting to back up a directory using PHP's system() function. My code looks something like this: $command='tar -cvzf /home/my_username/backups/backed_up_files.tar.gz /home/my_username/public_html/files_to_be_backed_up'; if (system($command) === FALSE) ...

  1. #1
    JPC Addict
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135

    50% CPU usage when using system function

    I'm attempting to back up a directory using PHP's system() function. My code looks something like this:

    $command='tar -cvzf /home/my_username/backups/backed_up_files.tar.gz /home/my_username/public_html/files_to_be_backed_up';

    if (system($command) === FALSE) {
    $status="Error';
    } else {
    $status='Success';
    }
    echo $status;

    What happens is this - the archive isn't created, I get neither an error or success message, and the process's CPU usage shoots up to about 50%, and stays there until I kill it manually.

    I'm fairly new to using system(), so it could well be the case that I'm unaware of something. Any ideas?

  2. #2
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,312
    I think it's waiting in tar for a list of files maybe?

    I don't see a space between the ".tar" and the "." at the end of the line.
    I also don't see a semi colon at the end of the first line. (You probably want to separate those commands into two separate calls anyway, so that you can isolate and report on the problem rather than just "Script encountered some error".)
    I am also confused about what you're trying to do to what files in what directory.
    I am also confused why you're using both the -z flag and a separate gz step.
    There's also a syntax problem with single quotes and double quotes in this script. ( $status="Error'; )

    So to answer the 50% question, php might be consuming the resources in a tight loop polling for the tar command to finish. Just a guess.

    Also, I'd try to stay away from using words as variables that might be or might become reserved words in the language (or any future language to which you might want to port the applciation.) Words like "command" and "status", for instance.

    So let's try this
    PHP Code:
    <?php
    // This will create the tarball named backed_up_files.tar
    $command_tar='tar -cvf /home/my_username/backups/backed_up_files.tar /home/my/username/directory_to_be_backed_up';

    // This will compress backed_up_files.tar and create backed_up_files.tar.gz
    $command_gz='gz /home/my_username/public_html/backed_up_files.tar';

    $my_status "Success";
    if (
    system($command_tar) === FALSE)
    {
       
    $my_status="Error in tar command";
    }
    else 
    {
       if (
    system($command_gz) === FALSE)
       {
          
    $my_status="Error in gz command";
       }
    }
    echo 
    $my_status;
    ?>
    You can probably do more with error reporting than I've done here. I don't use very many system() calls in php, and any I've done have been quick hacks, so I haven't done robust error reporting with it.

    Good luck!
    Last edited by Ron; 02-26-2006 at 05:54 AM.

  3. #3
    JPC Addict
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135
    Quote Originally Posted by Ron
    I don't see a space between the ".tar" and the "." at the end of the line.
    I also don't see a semi colon at the end of the first line.
    Not sure what you mean here - there is a semi-colon. Regardless, the command works fine when used within a cron job - so I'm guessing it must be the something to do with the context in which I'm employing it (system).
    Quote Originally Posted by Ron
    I am also confused why you're using both the -z flag and a separate gz step.
    As I say, I'm new to system commands, so fumbling my way a bit.
    Quote Originally Posted by Ron
    There's also a syntax problem with single quotes and double quotes in this script. ( $status="Error'; )
    Just a typo in my post, sorry.

    Quote Originally Posted by Ron

    So let's try this
    PHP Code:
    <?php
    // This will create the tarball named backed_up_files.tar
    $command_tar='tar -cvf /home/my_username/backups/backed_up_files.tar /home/my/username/directory_to_be_backed_up';

    // This will compress backed_up_files.tar and create backed_up_files.tar.gz
    $command_gz='gz /home/my_username/public_html/backed_up_files.tar';

    $my_status "Success";
    if (
    system($command_tar) === FALSE)
    {
       
    $my_status="Error in tar command";
    }
    else 
    {
       if (
    system($command_gz) === FALSE)
       {
          
    $my_status="Error in gz command";
       }
    }
    echo 
    $my_status;
    ?>
    Your individual commands also work in the context of cron. But when used as arguments in system(), they do nothing. It reports success, and doesn't tie up the CPU. But it doesn't create any archives at all.
    Last edited by uprightdog; 02-26-2006 at 06:23 AM.

  4. #4
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,312
    First off, PLEASE don't post code if it isn't the code with which you had the problem, or at least label it as not the same code.
    Not sure what you mean here - there is a semi-colon.
    Ok;, here is your line
    PHP Code:
    $command='tar -cvzf /home/my_username/backups/backed_up_files.tar. gz /home/my_username/public_html/files_to_be_backed_u p'
    It is trying to issue two commands, a tar command and a gz command. They need to be separated by a semi colon.

    Anyway, I ran my version, and it works just fine for me. Are you running this from the command line, or trying to execute it from a browser through the webserver? Do you or your webserver have permissions to write to the directory? Try the code with simple commands like
    PHP Code:
    $command_tar 'echo "I am in the tar command"> /home/my_username/dirname/tarball.tar'
    Then you can type
    zcat tarball.tar.gz
    and you should see
    I am in the tar command
    Here is a test I ran with the code first; the only changes are to obfuscate my username and directoryname.
    PHP Code:
    <?php
    $command_tar
    ='tar -cvf /home/my_username/dirname/tarball.tar /home/my_username/dirname/';

    $command_gz='gzip -v /home/my_username/dirname/tarball.tar';
    $command_ls='ls -alt /home/my_username/dirname/';
    system($command_ls);
    $my_status "Success";
    if (
    system($command_tar) === FALSE)
    {
       
    $my_status="Error in tar command";
    }
    else
    {
       if (
    system($command_gz) === FALSE)
       {
          
    $my_status="Error in gz command";
       }
    }
    echo 
    $my_status;
    system($command_ls);
    ?>
    $ php ron.php
    total 10848
    drwxr-xr-x 3 my_username my_username 4096 Feb 26 08:20 .
    drwx--x--x 30 my_username my_username 4096 Feb 26 08:20 ..
    -rw-r--r-- 1 my_username my_username 438 Feb 26 08:20 ron.php
    -rw-r--r-- 1 my_username my_username 11052773 Jan 28 20:16 ip.txt
    -rw-r--r-- 1 my_username my_username 227 Apr 11 2005 index.php
    drwxr-xr-x 3 my_username my_username 4096 Jul 22 2004 temp
    tar: Removing leading `/' from member names
    /home/my_username/dirname/
    /home/my_username/dirname/ip.txt
    /home/my_username/dirname/index.php
    /home/my_username/dirname/ron.php
    tar: /home/my_username/dirname/tarball.tar: file is the archive; not dumped
    /home/my_username/dirname/temp/
    /home/my_username/dirname/temp/bak/
    /home/my_username/dirname/temp/bak/3.php
    /home/my_username/dirname/temp/bak/1.php
    /home/my_username/dirname/temp/bak/2.php
    /home/my_username/dirname/temp/3.php
    /home/my_username/dirname/temp/1.php
    /home/my_username/dirname/temp/ron.awk
    /home/my_username/dirname/temp/ron.sed
    /home/my_username/dirname/temp/2.php
    /home/my_username/dirname/tarball.tar: 93.9% -- replaced with /home/my_username/dirname/tarball.tar.gz
    Success
    total 11520
    drwxr-xr-x 3 my_username my_username 4096 Feb 26 08:20 .
    -rw-r--r-- 1 my_username my_username 680012 Feb 26 08:20 tarball.tar.gz
    drwx--x--x 30 my_username my_username 4096 Feb 26 08:20 ..
    -rw-r--r-- 1 my_username my_username 438 Feb 26 08:20 ron.php
    -rw-r--r-- 1 my_username my_username 11052773 Jan 28 20:16 ip.txt
    -rw-r--r-- 1 my_username my_username 227 Apr 11 2005 index.php
    drwxr-xr-x 3 my_username my_username 4096 Jul 22 2004 temp

  5. #5
    JPC Addict
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135
    Quote Originally Posted by Ron
    First off, PLEASE don't post code if it isn't the code with which you had the problem, or at least label it as not the same code.
    Fair enough. Apologies.
    Quote Originally Posted by Ron
    Anyway, I ran my version, and it works just fine for me. Are you running this from the command line, or trying to execute it from a browser through the webserver? Do you or your webserver have permissions to write to the directory?
    It was indeed permissions. Your version now works.

    Thanks for your help Ron.

  6. #6
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,312
    Anytime.

    BTW, I don't think that checking for system() === FALSE will do as you're expecting.

    Here was output when I forgot to rename my_username/directoryname to the proper values:

    sh: /home/my_username/dirname/tarball.tar: No such file or directory
    gzip: /home/my_username/dirname/tarball.tar: No such file or directory
    Success
    You'll have to play around with the commands and their actual return values to get that construct to work.
    Last edited by Ron; 02-26-2006 at 08:10 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •