Solved

python automate script

Posted on 2014-04-10
12
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 38

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 38

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 38

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 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