1

I have a small script that checks certain condition continously and as soon as that condition is met the program should execute. Can this be done. I thought of using crontab where script runs every 5 min but now I want that to be done without crontab

Atul
  • 33
  • 1
  • 2
  • 6
  • See this accepted answer http://stackoverflow.com/questions/525247/how-do-i-daemonize-an-arbitrary-script-in-unix – tommasop May 04 '12 at 12:03
  • possible duplicate of [how can I continously run a unix script in background without using crontab.](http://stackoverflow.com/questions/10445201/how-can-i-continously-run-a-unix-script-in-background-without-using-crontab) – WrightsCS Jun 05 '12 at 03:07

1 Answers1

1

You probably want to create an infinite loop first, then within that loop you probably want to verify your condition or wait a bit. As you did not mention which scripting language you wanted to use, I'm going to write pseudo code for the example. Give us more info about the scripting language, and perhaps also the conditions.

Example in pseudo code:

# Defining a timeout of 1 sec, so checking the condition every second
timeout = 1

# Running in background infinitely
while true do
  # Let's check the condition
  if condition then
    # I got work to do
    ...
  else
    # Let's wait a specified timeout, not to block the system
    sleep timeout
  endif
endwhile

Example in sh with your input code

#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Defining a timeout of 1 hour
timeout=3600

while true
do
  case 'df /tmp' in
    " "[5-9]?%" ") rm -f /tmp/af.*
      ;;
    *)
      sleep $timeout
      ;;
  esac
done

You can then run this script from the shell using 'nohup':

nohup yourscript &
Huygens
  • 617
  • 1
  • 13
  • 26
  • Actually i have developed one script in unix that checks the directory space and if it exceeds certain limit it delets the file of that disk. Now I want that script run after continously or every 1hr.Can you please help me as I can't use crontab – Atul May 04 '12 at 12:16
  • Do you use bash script? ksh script? perl script? python script? etc. What do you mean "run after continuously"? Do you want it to be a daemon? To be run at startup? To continue running after you close your shell? – Huygens May 04 '12 at 12:20
  • here is the script#!/bin/sh PATH=/bin:/usr/bin:/sbin:/usr/sbin case 'df /tmp' in *" "[5-9]?%" "*) rm -f /tmp/af.*;; esac...This needs to run every say 1 hr – Atul May 04 '12 at 12:24
  • Using your script code, I've updated it to create the infinite loop, see answer. – Huygens May 04 '12 at 12:43
  • thanks Huygens...so this mean that I just have to edit my script and when I run this it will run even if I logout of my system? – Atul May 04 '12 at 12:47
  • If you update your script and you launch it with nohup, then yes it will continue running after you logout. – Huygens May 04 '12 at 12:54
  • hey i just tried nohup &, and if i do ps -a it shows some process. But if I logout and login again..ps -a doesent show that script? – Atul May 04 '12 at 13:32
  • It is normal, 'ps -a' won't show you the process once you logged out. Try 'ps wux' and you will see it (there is no '-' before 'wux'). – Huygens May 04 '12 at 14:25
  • @Atul did it fixed your question? Did you see your process still running using the ps command I gave you? – Huygens May 11 '12 at 08:25