How do you execute external programs from Python?

Discussion in 'Python' started by Carl, Nov 25, 2004.

  1. Carl

    Carl Guest

    Hi,

    I have this little code snippet that I use for recording audio streams. My
    problem is that I want to schedule my recordings with crontab. This does
    not work, however. I cannot figure out why; my code works fine when run
    manually from the command prompt.

    On final thing: What would be the best way to turn off the recording?

    Can someone help me?

    Carl


    Here is my code:

    import time
    import os
    import shutil

    URL = "http://www.jazzandblues.org/listen/links/kkjz1.ram"

    def RecordURL(what = "KKJZ"):
    str_mplayer = "mplayer -playlist " + URL
    str_mplayer += " -ao pcm -aofile "
    str_mplayer += what + ".wav" + " -vc dummy -vo null"
    os.system(str_mplayer)

    def CreateOGG(what = "KKJZ"):
    ogg_file = what + ".ogg"
    str_oggenc = "oggenc " + what + ".wav"
    os.system(str_oggenc)

    def CreateMP3(what = "KKJZ"):
    mp3_file = what + ".mp3"
    str_lame = "lame " + what + ".wav " + mp3_file
    os.system(str_lame)

    def DeleteWAV(what = "KKJZ"):
    wav_file = what + ".wav"
    str_rm = "rm " + wav_file
    os.system(str_rm)

    def DateTag(what = "KKJZ"):
    local_time = time.asctime(time.localtime())
    for local_file in [ what + ".ogg", what + ".mp3" ]:
    length = len(local_file)
    local_file_copy = local_file[: length - 4] + "_" + local_time + "."
    + local_file[length - 3 :]
    shutil.copy(local_file, local_file_copy)

    RecordURL()
    ## CreateOGG()
    ## CreateMP3()
    ## DeleteWAV()
    ## DateTag()

    Here is my crontab entry:

    30 * * * * python -c "import os; os.chdir('/home/alpha/mymusic/P2/Jazz/');
    import KKJZ"
     
    Carl, Nov 25, 2004
    #1
    1. Advertising

  2. You did not mentions what specific part is not working under cron, so
    here is a guess.

    Maybe the PATH environment variable is not set correctly such that
    binaries like mplayer can not be found.

    Try using absolute path names for mplayer and any other files.

    /Jean Brouwers


    In article <iGppd.2045$3N5.117@amstwist00>, Carl
    <> wrote:

    > Hi,
    >
    > I have this little code snippet that I use for recording audio streams. My
    > problem is that I want to schedule my recordings with crontab. This does
    > not work, however. I cannot figure out why; my code works fine when run
    > manually from the command prompt.
    >
    > On final thing: What would be the best way to turn off the recording?
    >
    > Can someone help me?
    >
    > Carl
    >
    >
    > Here is my code:
    >
    > import time
    > import os
    > import shutil
    >
    > URL = "http://www.jazzandblues.org/listen/links/kkjz1.ram"
    >
    > def RecordURL(what = "KKJZ"):
    > str_mplayer = "mplayer -playlist " + URL
    > str_mplayer += " -ao pcm -aofile "
    > str_mplayer += what + ".wav" + " -vc dummy -vo null"
    > os.system(str_mplayer)
    >
    > def CreateOGG(what = "KKJZ"):
    > ogg_file = what + ".ogg"
    > str_oggenc = "oggenc " + what + ".wav"
    > os.system(str_oggenc)
    >
    > def CreateMP3(what = "KKJZ"):
    > mp3_file = what + ".mp3"
    > str_lame = "lame " + what + ".wav " + mp3_file
    > os.system(str_lame)
    >
    > def DeleteWAV(what = "KKJZ"):
    > wav_file = what + ".wav"
    > str_rm = "rm " + wav_file
    > os.system(str_rm)
    >
    > def DateTag(what = "KKJZ"):
    > local_time = time.asctime(time.localtime())
    > for local_file in [ what + ".ogg", what + ".mp3" ]:
    > length = len(local_file)
    > local_file_copy = local_file[: length - 4] + "_" + local_time + "."
    > + local_file[length - 3 :]
    > shutil.copy(local_file, local_file_copy)
    >
    > RecordURL()
    > ## CreateOGG()
    > ## CreateMP3()
    > ## DeleteWAV()
    > ## DateTag()
    >
    > Here is my crontab entry:
    >
    > 30 * * * * python -c "import os; os.chdir('/home/alpha/mymusic/P2/Jazz/');
    > import KKJZ"
    >
    >
     
    Jean Brouwers, Nov 25, 2004
    #2
    1. Advertising

  3. Carl

    michael Guest

    > > Here is my crontab entry:
    > >
    > > 30 * * * * python -c "import os; os.chdir('/home/alpha/mymusic/P2/Jazz/');
    > > import KKJZ"


    Hi,

    if cron is running as root say

    30 * * * * "su - user; script.sh"

    su - user means that .profile, .bash-profile etc is read. I think its
    an "Environmental" Problem
     
    michael, Nov 26, 2004
    #3
  4. Carl

    Carl Guest

    Carl wrote:

    > Hi,
    >
    > I have this little code snippet that I use for recording audio streams. My
    > problem is that I want to schedule my recordings with crontab. This does
    > not work, however. I cannot figure out why; my code works fine when run
    > manually from the command prompt.
    >
    > On final thing: What would be the best way to turn off the recording?
    >
    > Can someone help me?
    >
    > Carl
    >
    >
    > Here is my code:
    >
    > import time
    > import os
    > import shutil
    >
    > URL = "http://www.jazzandblues.org/listen/links/kkjz1.ram"
    >
    > def RecordURL(what = "KKJZ"):
    > str_mplayer = "mplayer -playlist " + URL
    > str_mplayer += " -ao pcm -aofile "
    > str_mplayer += what + ".wav" + " -vc dummy -vo null"
    > os.system(str_mplayer)
    >
    > def CreateOGG(what = "KKJZ"):
    > ogg_file = what + ".ogg"
    > str_oggenc = "oggenc " + what + ".wav"
    > os.system(str_oggenc)
    >
    > def CreateMP3(what = "KKJZ"):
    > mp3_file = what + ".mp3"
    > str_lame = "lame " + what + ".wav " + mp3_file
    > os.system(str_lame)
    >
    > def DeleteWAV(what = "KKJZ"):
    > wav_file = what + ".wav"
    > str_rm = "rm " + wav_file
    > os.system(str_rm)
    >
    > def DateTag(what = "KKJZ"):
    > local_time = time.asctime(time.localtime())
    > for local_file in [ what + ".ogg", what + ".mp3" ]:
    > length = len(local_file)
    > local_file_copy = local_file[: length - 4] + "_" + local_time +
    > "."
    > + local_file[length - 3 :]
    > shutil.copy(local_file, local_file_copy)
    >
    > RecordURL()
    > ## CreateOGG()
    > ## CreateMP3()
    > ## DeleteWAV()
    > ## DateTag()
    >
    > Here is my crontab entry:
    >
    > 30 * * * * python -c "import os; os.chdir('/home/alpha/mymusic/P2/Jazz/');
    > import KKJZ"


    Thanks guys!

    It was an environmental problem. It works now; the only thing I still
    haven't solved is how to kill the spawned process after say an hour. Any
    clues?

    Carl
     
    Carl, Nov 27, 2004
    #4
  5. Carl

    Paul Watson Guest

    "Carl" <> wrote in message
    news:e_1qd.2176$3N5.2001@amstwist00...
    > Carl wrote:
    >
    > Thanks guys!
    >
    > It was an environmental problem. It works now; the only thing I still
    > haven't solved is how to kill the spawned process after say an hour. Any
    > clues?
    >
    > Carl


    Have you investigated...?

    os.spawn*()
    os.P_NOWAIT
    os.kill()
     
    Paul Watson, Nov 27, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,683
    Dave Thompson
    Jan 10, 2005
  2. Krisztian Kepes

    Python - Execute more programs

    Krisztian Kepes, Jun 30, 2003, in forum: Python
    Replies:
    3
    Views:
    431
    Bengt Richter
    Jul 1, 2003
  3. Bob=Moore
    Replies:
    10
    Views:
    2,804
  4. Coca
    Replies:
    7
    Views:
    764
    Aidan Grey
    Aug 24, 2004
  5. lone_eagle
    Replies:
    3
    Views:
    660
    psykeedelik
    May 26, 2009
Loading...

Share This Page