Solved

python automate script

Posted on 2014-04-10
12
593 Views
Last Modified: 2014-05-07
i have a python script test.py
currently i am using on windows cmd test.py > output.txt

i want another script to auto run test.py every 15 seconds and output file will be random file name, example outpout1.txt, output2.txt

can someone help me to write this script, or if anyone has script pls share with me
0
Comment
Question by:ammadeyy2020
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39992822
I don't understand why you've selected MS SQL and Linux topics. You want to run the script on Windows or Linux? How 'random' do you want the filenames, must they be unique or do you just want a new filename every run?
0
 

Author Comment

by:ammadeyy2020
ID: 39992852
new file name every run
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 39992890
This will run your test.py 100 times:
let a=0
while [ $a -lt 100 ]
do
  test.py > output$a.txt
  let a=$a+1
done ;

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39992907
You'll want to read up on the sched and subprocess modules. The former should allow you to schedule 15-second intervals; the latter should allow you to execute the other script.

e.g.

import subprocess
import sched
import time

s = sched.scheduler(time.time, time.sleep)

def worker(scheduler, interval):
    with open('output' + str(interval) + '.txt', 'w') as out:
        subprocess.check_call("python test.py", shell=True, stdout=out)
        
    # Restart timer at end of function
    s.enter(15, 1, worker, (scheduler, interval + 1))
    s.run()
    
###  Start timer

s.enter(15, 1, worker, (s, 1))
s.run()

Open in new window


In the above, you basically continually call worker. worker passes itself to the enter method, so you continually execute worker in this fashion. The work gets done in the first two line of worker's body.
0
 

Author Comment

by:ammadeyy2020
ID: 39992910
let a=0
syntax error

pls check attachment
leta.PNG
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39992929
I suggested a Linux script, did you read the all the questions in my first post?
0
 

Author Comment

by:ammadeyy2020
ID: 39992980
./auto.py: line 1: $'\r': command not found
./auto.py: line 2: $'\r': command not found
")syntax error: invalid arithmetic operator (error token is "
./auto.py: line 8: syntax error near unexpected token `done'
./auto.py: line 8: `done ;'

that is the error i get in linux
0
 

Author Comment

by:ammadeyy2020
ID: 39993044
syntax error near unexpected token `done'
 line 8: `done ;'
0
 

Author Comment

by:ammadeyy2020
ID: 39993048
i copied exact samething

let a=0
while [ $a -lt 100 ]
do
  test.py > output$a.txt
  let a=$a+1
done ;

and getting error
 syntax error near unexpected token `done'
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39993134
First, you said:

i want another script...

...which to me the word "another" would imply that you wanted an additional Python script.

Second, you said:

...to auto run test.py every 15 seconds...

Gerwin Jansen's shell script isn't doing that; it's running a fixed number of iterations, one right after the other. So now I'm confused:  What is it that you want?
0
 
LVL 5

Expert Comment

by:Pasha Kravtsov
ID: 40006904
@ammadeyy2020
You need to add
#!/usr/bin/python2

Open in new window

as the first line of your code for python if you run it as an executable :)
you could alternatively use
python2 code.py

Open in new window


and for you bash script (I'm assuming you're doing the same thing) add this to the first line:
#!/bin/bash

Open in new window


when you do ./ the computer doesn't know what kind of language it'll be compiling/running so you need to designate this by using #!/path/to/executable/
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

832 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