[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

python automate script

Posted on 2014-04-10
12
Medium Priority
?
623 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Fine Tune your automatic Updates for Ubuntu / Debian
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

650 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