Automatically executing a command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Automatically executing a command
# 1  
Old 04-06-2005
Automatically executing a command

Hi

I was wondering if anyone knew the answer to this question?

I am trying to find a way of executing a command if a certain file is created in the same directory.

One way I thought about doing this was to create a FORTRAN program that continually searches for this file. If the file exists then the FORTRAN program executes the command.

The problem with this is that I am worried that I will be utilising a whole CPU and am worried the CPU might go crazy!

So can anyone think of another way I could do this?
For example could I use a daemon that executes the command once the file exists? If so, how exactly can I set it up?

Or does anyone know how I could limit the CPU power for the FORTRAN program?
(I have tried the nice / renice commands but these will only limit the CPU power if all of the CPUs are being used.)

Thanks in advance
# 2  
Old 04-06-2005
you can do it more simply with a script and using cron to run the script every few minutes.

Use the find command and -prune statement

find . -name "file" -prune {} \;


What type of command is it? Could you do it all in a shell script?
# 3  
Old 04-06-2005
to forever look for the file in the directory, you can use ...
Code:
#! /bin/ksh

while true
do
    if [ -f /dir/file ]
    then
          echo "i found the file and it is here!"
    else
          sleep 1
    if
done

exit 0

or if you want the script to take a break or exit after it sees the file in the directory so you can run other scripts on it ...
Code:
#! /bin/ksh

while true
do
    if [ -f /dir/file ]
    then
          echo "i found the file and it is here!"
          break # replace with "exit" as required
    else
          sleep 1
    if
done

exit 0

# 4  
Old 04-06-2005
Thanks for your reply.

Your idea sounds good however I would like to check whether the file is there or not more instantaneously. I don't think you can specify a time of less than a minute with CRON. Would you know of another way of doing this?

The command itself is just a batch script.
# 5  
Old 04-06-2005
Look at Just Ice's solution above. The sleep command allows you to specify time in seconds, so that you can have the script running (almost) all the time without unneccessarily taxing your system.
# 6  
Old 04-06-2005
the sleep line is set to 1 second which is the shortest you can set it to in ksh but i heard perl might be able to get you something less than a second ... the more advanced perl users here should be able to steer you in the right direction ...
# 7  
Old 04-06-2005
Thanks all,

I took Just Ice's advice and made a csh script with a sleep of 1 second. It is way more elegant than the FORTRAN program however it would be excellent if the sleep command could use a value of less than 1 second (say 0.1 seconds). So if anyone has any ideas, please reply!?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Warn Before Executing Particular Command

I'm running CentOS 6.8 and use bash. I would like a warning to appear to the user who runs the command "service httpd restart" E.g. # service httpd restart are you sure y/n n # (or if y, the command executes). I looked into it a little but am not sure of the best approach. Aliases I... (2 Replies)
Discussion started by: spacegoose
2 Replies

2. Shell Programming and Scripting

Automatically enter input in command line

Hi, This is a script which to create an opvn user, I want which answer automatically to a certain part so, I try this, it works without the red part but I must type manually.. : #!/bin/bash ## Environnement ## LC_ALL=C ## Paths ## rsa_dir="etc/openvpn/easy-rsa"... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

3. Shell Programming and Scripting

In put password automatically to sftp command

Hi all, i have to sftp a file to another server I don't have "expect" or sshpass on my machine its solaris 10, i want to pass password in command line or after doing this sftp user@server how to automatically provide the password as input ( stored in some variable or so) ... (1 Reply)
Discussion started by: zozoo
1 Replies

4. Shell Programming and Scripting

Automatically invocation of unix command

Dear All, I have a directory when i received files by means of FTP, i want to invoke my shell scripts as soon as file hit the directory, if the files hit 10 times then the shell scripts should also get executed 10 times. thanks rajesh (1 Reply)
Discussion started by: guddu_12
1 Replies

5. Shell Programming and Scripting

cd command not executing in if else loop

Hi, I am executing below script s1=`pwd` s2=/space if then echo "done" else echo "mistake" cd /tmp fi I am not able to migrate to /tmp directory if the condition is not true.However mistake is being printed.Means cd command is not working here.All other commands except cd are... (3 Replies)
Discussion started by: d8011
3 Replies

6. UNIX for Dummies Questions & Answers

xterm closing automatically after command excution

Hi, I have a commands in a file called commands.file, I am using this file with the following commnad xterm -e "commands.file" After executing this file in the new xterm, it is closing automatically. I want to use that xterm after that. Please help me regarding this? :) ... (4 Replies)
Discussion started by: chaitubek
4 Replies

7. Shell Programming and Scripting

automatically answer a question raised by a command

i have installed vmware on a text base linux node now i have to vmware-configure.pl to do the initial configuration now 1st step it askes for agreeing for a " License Agreement" for that i have to say "q" and "yes" to Accept it i want to run a script with does these 3 steps... (6 Replies)
Discussion started by: pbsrinivas
6 Replies

8. UNIX and Linux Applications

How to automatically detect command failure

I have a shell script. In this script I executes various command and my requirement is such that if any command fails I've to terminate the shell script. To achieve this objective I'm checking the value of $? after each command and if its value is greater thaen I 'exit' the script. Is there... (2 Replies)
Discussion started by: ashok2008
2 Replies

9. Shell Programming and Scripting

run a command automatically after every 10 mins

Hi friends, I need to write a script which runs a command (on that particular server only) after every 10 mins, and the contents are also echoed on that very terminal only. And then I need to compare the last two outputs, and mail if there is any difference in the last two outputs. Can we set... (4 Replies)
Discussion started by: vikas027
4 Replies

10. UNIX for Dummies Questions & Answers

Executing a unix command

Hi, I need to execute the following unix command through my java code - zip -e When i execute this command from the command prompt, i am prompted for a password in the following manner - Enter password: Verify password: Is it possible to provide the password inthe first command itself... (5 Replies)
Discussion started by: jacob23
5 Replies
Login or Register to Ask a Question