Solved

python automate script

Posted on 2014-04-10
12
586 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
Comment Utility
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
Comment Utility
new file name every run
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
let a=0
syntax error

pls check attachment
leta.PNG
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
I suggested a Linux script, did you read the all the questions in my first post?
0
 

Author Comment

by:ammadeyy2020
Comment Utility
./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
Comment Utility
syntax error near unexpected token `done'
 line 8: `done ;'
0
 

Author Comment

by:ammadeyy2020
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
@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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now