Shell script that runs python script again when it's finished [duplicate]
Solution 1:
You can use watch
command, watch is used to run any designated command at regular intervals.
Open Terminal and type:
watch -n x <your command>
change x to be the time in seconds you want.
For more help using the watch
command and its options, run man watch
or visit this Link.
For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:
watch -n 60 ls -l ~/Desktop
Solution 2:
You can also use this command in terminal, apart from nux's answer:
while true; do <your_command>; sleep <interval_in_seconds>; done
Example:
while true; do ls; sleep 2; done
This command will print the output of ls
at an interval of 2 sec.
Use Ctrl+C to stop the process.
There are few drawbacks of watch
:
- It cannot use any aliased commands.
- If the output of any command is quite long, scrolling does not work properly.
- There is some trouble to set the maximum time interval beyond a certain value.
-
watch
will interpret ANSI color sequences passing escape characters using-c
or--color
option. For example output ofpygmentize
will work but it will fail forls --color=auto
.
In the above circumstances this may appear as a better option.
Solution 3:
Just wanted to pitch in to sourav c.'s and nux's answers:
-
While
watch
will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure. On FreeBSD for example,watch
is a command to "snoop on another tty line". -
while true; do command; sleep SECONDS; done
also has a caveat: your command might be harder to kill using Ctrl+C. You might prefer:while sleep SECONDS; do command; done
It's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some
SECONDS
before the first occurrence of the command will happen.
Solution 4:
Sounds like the ideal task for the cron
daemon which allows for running periodic commands. Run the crontab -e
command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:
The time and date fields are:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sunday, or use names)
For example, if you would like to run a Python script on every Tuesday, 11 AM:
0 11 * * 1 python ~/yourscript.py
There are also some special names that replace the time, like @reboot
. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l
):
# Creates a temporary directory for ~/.distcc at boot
@reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"
Solution 5:
If you are monitoring the file system, then inotifywait
is brilliant and certainly adds less load on your system.
Example :
In 1st terminal type this command :
$ inotifywait .
Then in 2nd terminal, any command that affects the current directory,
$ touch newfile
Then in original terminal inotifywait will wake up and report the event
./ CREATE newfile2
Or in a loop
$ while true ; do inotifywait . ; done
Setting up watches.
Watches established.
./ OPEN newfile2
Setting up watches.
Watches established.
./ OPEN newfile2
Setting up watches.
Watches established.
./ DELETE newfile
Setting up watches.
Watches established.
./ CREATE,ISDIR newdir
Setting up watches.
Watches established.