[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to execute (python) script in crontab with command line arguments

Posted on 2008-11-06
7
Medium Priority
?
2,220 Views
Last Modified: 2013-12-26
I have a python script that processes data files which I currently run from the Linux command line:
$ /path/process.py archive1.csv archive1 2> error.log
with arguments: name_of_file to process, subdirectory for the output file, and 2> error.log catches any errors
Each of 24 archives takes at least 60 seconds to process; I also have 24 data files which take 15 - 30 seconds each to process.  I've tried to get the python script to run in my crontab iteratively for each archive file but nothing happens.  I've tried integrating the command line arguments into a bash script with similar results, probably because I don't know enough about bash scripting.

The alternative to a bash script would be to change the python script so that it will run as is from the crontab, but that looks more difficult, so I thought I'd try this approach first.
 
0
Comment
Question by:sara_bellum
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 22901718
to run crontab jobs,

- set env variables needed in the script
- use full path names to files, dirs, and commands
- change to required dir

you may have a script that will loop for each file to process



0
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 22901751
I'd write a bash wrapper script, eg:
#!/bin/bash
for i in $(seq 1 24)
do
   /path/process.py archive$i.csv archive$i 2>>/path/to/error.log
done

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22901756
If your system doesn't have the seq command, then change the above to:
#!/bin/bash
i=1
 
while [ $i -le 24 ]
do
  /path/process.py archive$i.csv archive$i 2>>/path/to/error.log
  let i++
done

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:sara_bellum
ID: 22910395
Thanks very much!  I have the seq command so the first bash wrapper script worked :)  I spent the better part of today trying to upload the results, but that would be a lot easier if all of my output files were in one directory.  So I wrote another bash wrapper (copied below) to move the 2 output files for each data1 - 24 folder from /path/data$i to /path/test/.  But this time (because I wrote it) the paths are not recognized - the error reads "cannot stat '/path/data$i/data$i_filename' No such file or directory" for each of 48 iterations.  Let me know what I'm doing wrong, or whether I should open another question, thanks.  

#!/bin/bash
 
SRC='/path1/data$i'
DST='/path2/test'
src1='data$i_1_minutes.csv'
src2='data$i_60_minutes.csv'
dst1='data$i_minutes.csv'
dst2='data$i_hours.csv'
for i in $(seq 1 24)
do
  mv $SRC/$src1 $DST/$dst1
  mv $SRC/$src2 $DST/$dst2
done

Open in new window

0
 

Author Comment

by:sara_bellum
ID: 22910417
I should note that the source and destination paths are correct, but the system isn't translating data$i into folder names data1 - data24, and so the filenames of course have the same problem.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 2000 total points
ID: 22911147
From your description and last script, you should just need:
#!/bin/bash
for i in $(seq 1 24)
do
  mv /path1/data$i/data${i}_minutes.csv /path2/test
  mv /path/data$i/data${i}_hours.csv /path2/test
done

Open in new window

0
 

Author Closing Comment

by:sara_bellum
ID: 31514633
Brilliant!! I'm still confused as to why bash can't interpret $i when it's in a pre-defined variable (it would be nice to know why) but I'm very happy with this result, thanks again.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question