Hi, first I have a doubt in order to run a single script several times at same time I used this:
Where n is the times will run the program at the same time.
Is it this correct?
Second, I have a script that read a text file and modify it and I want to simulate a semaphore in order to have the same script running several times at the same time and just one can edit and when it finish the second will edit and so on..., but in ksh I can't use lock file... So on internet I look for another way and I found that it can be do it with mkdir cos is an atomic operation, so I implemented on my code.
But it doesn't work, and the sentence that is in red I don't understand very well.
Cos Always I run the program never enter to the sleeep, so I think the if always is true and then it's breaks the loop, but I'm not sure.
And also for some reason it doesn't create the directory... I don't know why
Yes, your for loop will start n concurrent executions of the same script.
The statement you have in red attempts to make a directory. If it is successful, the if should evaluate to true and the loop should be exited. I ran your lock wait function and it works for me.
As an alternative example, here is a stripped down version of what I use. Similar, but different enough to give you a second perspective. You can try running it twice from the command line to see if it is able to create a lock and hold it for a while.
In the code that I use in production I allow for a max amount of time before the lock function gives up and returns an error. It also supports a no-wait option that allows for a non-blocking approach.
The statement you have in red attempts to make a directory. If it is successful, the if should evaluate to true and the loop should be exited. I ran your lock wait function and it works for me.
Then my pc hates me XDD, ok no xD
But then if is creating a directory and it is successful the loop will be over and never enter to sleep??? or it is the opposite??
But it's very strange that it is not creating any directory for me....
if I put the next:
is it still the same condition?
I will try this and then I will edit xD
----edit
Ok I tried it, now It's creating the directory, but I think never will enter to the sleep xDDD
Also I have another question, seeing your example, sometimes I tried to do a statment like this one:
But when I run the program it says that !: not found, why is that?
But then if is creating a directory and it is successful the loop will be over and never enter to sleep??? or it is the opposite??
If it is successful on the first try it should not enter the loop and once it is successful it should exit the loop.
Quote:
Ok I tried it, now It's creating the directory, but I think never will enter to the sleep xDDD
Great ! Try running two of them at the same time. The first should create the directory and the second should block until the first frees the lock.
Quote:
Also I have another question, seeing your example, sometimes I tried to do a statment like this one:
But when I run the program it says that !: not found, why is that?
That sounds like an old version of the shell. What is the output of the command
I think you were using kshell. If you have an older version, before 2002/02 you might want to install a new one.
yes I'm using ksh, I think It will be a little dificult for me cos it's has to work in that old version..... e_e but well I'll don't give up until I tried everything xD
Quote:
Great ! Try running two of them at the same time. The first should create the directory and the second should block until the first frees the lock.
I did that with five... so I think it isn't working.... damn old version ksh ¬¬
I was reading something about using a "lockfile" but no the lockfile that needs to install a package in order to use it, it is a hand made lock file.
So I rework on my code and try something:
About what I read I guess I need to have this: while exist the lock file the process needs to wait, create the lockfile, do the stuff of the main script, delete the lockfile and repite the process I think....
at first sight i think is working... but I think never enter to the sleep, I never see the "sleeping"
But now I have a doubt, I really don't know if this:
is it working well?
cos when I put:
just appears 1... I think it has to appears the n process that I run right?
is it another way to run a script n times at same time?
The lock file code in your last post is bound to have problems. If two instances of your script both notice that the lock file is missing at the same time, they will both touch the lock file and go on. Not that it matters in this case, since touch will always work, you don't check the status of the touch. What ever command you use to 'create' the lock file must allow only one process to be successful and the script must test the result and loop back to try again when it detects a failure.
The reason mkdir was used is because only one process will successfully create the directory if two attempt the operation at exactly the same time. Also, the return value of mkdir is also being checked and if it didn't succeed the script assumes it does not own the lock.
Assuming the seq command is installed on your machine, you could run this to start 5 processes:
Here is a version of my original code in what I think should execute under a very old version of ksh. It starts 5 copies of itself and each waits to get the lock reporting when it does and does on the tty. Cut and paste it into its own file and run it (just once) to see what happens. If this doesn't work (it does on my system) then I'm not sure what to do to help you along.
the seq isn't working on my ksh version and I can't installed, but I make this:
And it's working ^^
I tried to do something similar to your program where is the seq, but i think it didn't work, it never enter to the if, but the functions are working.
now I have a doubt, when I run my script, one process got the lock, the others just are waiting and the end they got the lock and don't respect my condition to stop at X number.
Hi All,
I want to write a domain specific REST/HTTP server to run on FreeBSD. I have control over both the server side and the primary client intended to consume the service.
My question is: how do you think it should be designed to support multiple connections, given:
It will run on a... (6 Replies)
Hi,
I am a mid-career programmer with extensive experience in object-oriented design and development in C, C++, and C#. I've written a number of multi-threaded server applications and background services, although my grasp of networking protocols is a bit weak: my current job drifted away from... (2 Replies)
I'm getting different behaviour when executing below script in debug option.
$ cat ss.ksh
ff=$(pwd)
echo " ff : $ff"
$ ksh ss.ksh
ff : /tmp
$ ksh -x ss.ksh
+ + pwd
ff=
+ echo ff :
ff :
I was getting this behaviour in my actuall script i'm able to reproduce this in simple script... (4 Replies)
Hi ,
What is the diffence between executing the script like
./myscript.ksh
. ./myscript.ksh
I have found 2 difference but could not find the reason
1. If i export a variable in myscript.ksh and execute it like . ./myscript.ksh the i can access the other scripts that are present in... (5 Replies)
Hi
I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email.
For example
-------
Script ABC
-------
a.ksh
b.ksh
c.ksh
I need to call all three scripts execute them and... (2 Replies)
Ih all,
i have multiples ksh scripts for crontab's unix jobs
they all have same variables declarations and some similar functions
i would have a only single script file to declare my variables, like:
var1= "aaa"
var2= "bbb"
var3= "ccc"
...
function ab { ...}
function bc { ... }... (2 Replies)
Hi Bros,
I am in a serious trouble with concurrency issue while using mailx. I have a shell script which reads all the emails of a unix user account and create a copy (another mbox for processing and archive purpose). This script works fine for 99.99% of the time but sometime it start creating... (2 Replies)