472,373 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Python, subprocess, dump, gzip and Cron

Hi,

I'm having a bit of trouble with a python script I wrote, though I'm not
sure if it's related directly to python, or one of the other software
packages...

The situation is that I'm trying to create a system backup script that
creates an image of the system, filters the output though gzip, and then
uploads the data (via ftp) to a remote site.

The problem is that when I run the script from the command line, it
works as I expect it, but when it is run by cron I only get a 20 byte
file where the compressed image should be... does anyone have any idea
as to why this might be happening? Code follows

<code>

#!/usr/bin/python

from subprocess import PIPE, Popen
from ftplib import FTP

host = 'box'

filename = '%s.img.gz' % host
ftp_host = '192.168.1.250'
ftpuser, ftppass = 'admin', 'admin'
dest_dir = '/share/%s' % host

dump = Popen('dump 0uaf - /',shell=True,stdout=PIPE)
gzip = Popen('gzip',shell=True,stdin=dump.stdout,stdout=P IPE)

ftp = FTP(ftp_host)
ftp.login(ftpuser,ftppass)
ftp.cwd(dest_dir)
ftp.storbinary('STOR %s' % filename,gzip.stdout)
ftp.quit()

print "Image '%s' created" % filename

</code>

I appreciate all feedback. Thanks in advance.
Jun 27 '08 #1
4 3812
TT
On Jun 10, 2:37*pm, Aidan <awe...@gmail.comwrote:
Hi,

I'm having a bit of trouble with a python script I wrote, though I'm not
sure if it's related directly to python, or one of the other software
packages...

The situation is that I'm trying to create a system backup script that
creates an image of the system, filters the output though gzip, and then
uploads the data (via ftp) to a remote site.

The problem is that when I run the script from the command line, it
works as I expect it, but when it is run by cron I only get a 20 byte
file where the compressed image should be... *does anyone have any idea
as to why this might be happening? *Code follows

<code>

#!/usr/bin/python

from subprocess import PIPE, Popen
from ftplib import FTP

host = 'box'

filename = '%s.img.gz' % host
ftp_host = '192.168.1.250'
ftpuser, ftppass = 'admin', 'admin'
dest_dir = '/share/%s' % host

dump = Popen('dump 0uaf - /',shell=True,stdout=PIPE)
gzip = Popen('gzip',shell=True,stdin=dump.stdout,stdout=P IPE)

ftp = FTP(ftp_host)
ftp.login(ftpuser,ftppass)
ftp.cwd(dest_dir)
ftp.storbinary('STOR %s' % filename,gzip.stdout)
ftp.quit()

print "Image '%s' created" % filename

</code>

I appreciate all feedback. *Thanks in advance.
it's possible that the cron doesn't have the environment variables you
have, especially $PATH. So the script failed to find the command it
need to create the image.
Jun 27 '08 #2
TT wrote:
On Jun 10, 2:37 pm, Aidan <awe...@gmail.comwrote:
>Hi,

I'm having a bit of trouble with a python script I wrote, though I'm not
sure if it's related directly to python, or one of the other software
packages...

The situation is that I'm trying to create a system backup script that
creates an image of the system, filters the output though gzip, and then
uploads the data (via ftp) to a remote site.

The problem is that when I run the script from the command line, it
works as I expect it, but when it is run by cron I only get a 20 byte
file where the compressed image should be... does anyone have any idea
as to why this might be happening? Code follows

<code>

#!/usr/bin/python

from subprocess import PIPE, Popen
from ftplib import FTP

host = 'box'

filename = '%s.img.gz' % host
ftp_host = '192.168.1.250'
ftpuser, ftppass = 'admin', 'admin'
dest_dir = '/share/%s' % host

dump = Popen('dump 0uaf - /',shell=True,stdout=PIPE)
gzip = Popen('gzip',shell=True,stdin=dump.stdout,stdout=P IPE)

ftp = FTP(ftp_host)
ftp.login(ftpuser,ftppass)
ftp.cwd(dest_dir)
ftp.storbinary('STOR %s' % filename,gzip.stdout)
ftp.quit()

print "Image '%s' created" % filename

</code>

I appreciate all feedback. Thanks in advance.

it's possible that the cron doesn't have the environment variables you
have, especially $PATH. So the script failed to find the command it
need to create the image.
*fore head slap*

Of course... adding the full path to both those utilities on the Popen
lines seems to have fixed it.

Thank you very much for your assistance.
Jun 27 '08 #3
Aidan <aw****@gmail.comat Dienstag 10 Juni 2008 07:21:
TT wrote:
>On Jun 10, 2:37 pm, Aidan <awe...@gmail.comwrote:
>>Hi,

I'm having a bit of trouble with a python script I wrote, though I'm not
sure if it's related directly to python, or one of the other software
packages...

The situation is that I'm trying to create a system backup script that
creates an image of the system, filters the output though gzip, and then
uploads the data (via ftp) to a remote site.

The problem is that when I run the script from the command line, it
works as I expect it, but when it is run by cron I only get a 20 byte
file where the compressed image should be... does anyone have any idea
as to why this might be happening? Code follows

<code>

#!/usr/bin/python

from subprocess import PIPE, Popen
from ftplib import FTP

host = 'box'

filename = '%s.img.gz' % host
ftp_host = '192.168.1.250'
ftpuser, ftppass = 'admin', 'admin'
dest_dir = '/share/%s' % host

dump = Popen('dump 0uaf - /',shell=True,stdout=PIPE)
You should avoid the use of ``shell=True`` here and use a argument list
instead:

dump = Popen(['dump', '0uaf', '-', '/'], stdout=PIPE)

This results in an exception thrown if the executable doesn't exist. This
exception can be caught and handle for instance with the logging module.
>>gzip = Popen('gzip',shell=True,stdin=dump.stdout,stdout=P IPE)
Same here, but why don't you use the gzip functionality from the standard
library?

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jun 27 '08 #4
Sebastian "lunar" Wiesner wrote:
Aidan <aw****@gmail.comat Dienstag 10 Juni 2008 07:21:
>TT wrote:
>>On Jun 10, 2:37 pm, Aidan <awe...@gmail.comwrote:
Hi,

I'm having a bit of trouble with a python script I wrote, though I'm not
sure if it's related directly to python, or one of the other software
packages...

The situation is that I'm trying to create a system backup script that
creates an image of the system, filters the output though gzip, and then
uploads the data (via ftp) to a remote site.

The problem is that when I run the script from the command line, it
works as I expect it, but when it is run by cron I only get a 20 byte
file where the compressed image should be... does anyone have any idea
as to why this might be happening? Code follows

<code>

#!/usr/bin/python

from subprocess import PIPE, Popen
from ftplib import FTP

host = 'box'

filename = '%s.img.gz' % host
ftp_host = '192.168.1.250'
ftpuser, ftppass = 'admin', 'admin'
dest_dir = '/share/%s' % host

dump = Popen('dump 0uaf - /',shell=True,stdout=PIPE)
You should avoid the use of ``shell=True`` here and use a argument list
instead:

dump = Popen(['dump', '0uaf', '-', '/'], stdout=PIPE)

This results in an exception thrown if the executable doesn't exist. This
exception can be caught and handle for instance with the logging module.
thanks. That exception certainly would have helped me...
>>>gzip = Popen('gzip',shell=True,stdin=dump.stdout,stdout=P IPE)

Same here, but why don't you use the gzip functionality from the standard
library?
is there a way I can create a gzip file-like object which can read the
output from the dump subprocess, and has a read method which outputs the
compressed data, which will not write to disk first? With the above
code python doesn't have to write the system image data to disk at all,
which helps when there is not enough disk space to hold an intermediate
image (at least, that is my understanding of it...).

I had a look at the gzip module, but eventually just fell back to using
the stdin and stdout of a gzip subprocess. I'd be interested to know
how it could be done using the python standard lib though.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David Abrahams | last post by:
I started having some weird problems with Python recently; they're so weird that I can't begin to explain them. All I can do is describe the symptoms and hope someone else has a clue. So here...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
14
by: Bill | last post by:
I've written a small program that, in part, reads in a file and parses it. Sometimes, the file is gzipped. The code that I use to get the file object is like so: if filename.endswith(".gz"):...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open...
1
by: symbioid | last post by:
Hello, I'm working on a project, and VMware has problems with suspending the virtual machine. We are accessing the machine through samba. However, when I suspend the VM, it stops the Samba...
1
by: Steven Bethard | last post by:
I'm having trouble using the subprocess module on Windows when my command line includes special characters like "&" (ampersand):: .... stdout=subprocess.PIPE, .... ...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.