I have a requirement to do a script for gather some data from one db and formatting according to a requirement, this should run by a scheduled cron job. but there should not be parallel runs. (If one process is running, then other should not start).  Help me to do this in bash scripting.
123528
A solution that does not require additional tools would be prefered.
27430
I have some conditions for some background job to run:  condition-command &amp;&amp; condition-command &amp;&amp; background-job &amp;   The problem is: I want the conditions to block until the job runs, as if I had run:  condition-command; condition-command; background-job &amp;   But it isn't a condition, if previous command fails I do not want the job to run.  I realised it is asynchronous, but it should not, in my mind the both following scripts should be the same, but they do not:  sleep 2; echo foo &amp; sleep 1; echo bar; wait   # prints foo, then bar: correct sleep 2 &amp;&amp; echo foo &amp; sleep 1; echo bar; wait # prints bar, then foo: bug   I know if I test $? variable it would work, or if I put the last one inside a subshell (but then I would lost job controls, I want to avoid daemons), but I want to know why bash make it this way, where is it documented? Is there any way to prevent this behaviour?  Edit: Chained ifs is disgusting, that is why I will not accept it as a alternative way. Edit 2: I know a subshell is possible, but it will not work for me, let us imagine I want to run a bunch of commands then wait in the end. It will be possible if i check the existence of /proc/$PID directory, but it would be a pain in the neck if there are several jobs. Edit 3: The main question is WHY bash does it, where is it documented? Whether or not it has a solution is a bonus!
69590
I've recently run across multiple needs to execute multiple different batch jobs at the same time, and almost immediately hit the (somewhat inexplicable) SFDC limit of 5 batch jobs queued at once.  I've worked around this limit in a number of ways, none of which seem ideal to me.   returning a Iterable&lt;SObject&gt; from the start method, which contains a mix of all the SObjects I need to act upon, and then use dynamic inspection to figure out what I'm supposed to be doing on each row in the execute method. This seems fine for small jobs, but wouldn't presumably scale to millions of records due to heap size etc. using a scheduled apex job that runs once, executes a batch, and then that batch in its finish method schedules another apex job (to run in say 1 minute) that executes the next batch in the chain, and so on.   Is there a more viable/flexible pattern that I could be using? What I really need is a flexible work queue where on-demand, not-very-time-sensitive batch jobs can be queued for future (serial) execution based on user activity. And that doesn't violate platform limits of batch jobs or scheduled jobs - really needs to contribute a max of 1 scheduled or batch job at any given point.  Not sure it will help, but the characteristics of the job set I'm envisioning are this:   each task executes on a single SObject type and does one job: e.g. setting unset fields, doing fake M-D rollups, etc. some tasks are operating on a small subset of a table, others need to walk over every Contact/Lead/etc. with potentially millions of rows per batch job. while some tasks are known to be needed every N hours / days, most of them are needed only after some user-driven operation: e.g. click a big red button in an admin console, do something to more than N records, or a new/updated record of a certain type requires its downstream records to be operated upon.
121643
I have parent folder "parent". Inside this folder I have subfolders, and a file named "names.txt". this file includes the names of these subfolders as follow:  Parent_folder folder1 folder2 folder3 folder4 . . . . names.txt   The content of the file "names.txt" is as follow:  folder1 folder2 folder3 folder4 . . .   Inside every folder I have images and I want to apply consecutively 10 scripts on every image ( every script must finish it is job inside every folder, then the second script must be run). These scripts have different names and they are exist in one folder. I set an environment by sourcing a file then I can call these scripts by its name from terminal .At the same time I want to apply this process on all the folders at once. i.e. when script #1 is running, I want it to be running on all the folders at the same time. When it is done and script #2 will start. I want it to start in all the folders at once and so on... In order to achieve this I wrote the following code:  #!/bin/bash path=PATH/TO/THE/PARENT/FOLDER for i in $(cat $path/names.txt); do { script#1 } &amp; { script#2 } &amp; . . .  done   This code is not working efficiently as all the commands are running at once. I want the commands to run on all the folders at once but consecutively.  What I am doing wrong?
50714
I'd like to run multiple MySQL queries and save them to specified files. Queries are saved in a bash script, queries.sh, e.g.:  cat queries.sh mysql -u &lt;user&gt; -p &lt;DBname&gt; -e "select Col1 from Table1 where Condition;" &gt; /home/&lt;user&gt;/queries/Col1Table1Cond mysql -u &lt;user&gt; -p &lt;DBname&gt; -e "select Col5 from Table2 where Condition;" &gt; /home/&lt;user&gt;/queries/Col5Table2Cond   Executing the script is not enough, since &lt;user&gt; has to input his password at each query and this is compromising the script flow.
88450
I have a cron job on CentOS that I want to execute every 3 minutes but I have many other cronjobs that run at every 3 minutes starting from 0, 3, 6, 9...  So, to avoid my server getting too overloaded, I wanted some of my crons to run at every 3 minutes but starting at 1 minute and so on: 1, 4, 7...  My crons are usually like this:  */3 * * * *    How can I do this?
122372
I have some functions that use curl to pull information off a couple of sites and insert them into my database. I was just wondering what is the best way to go about executing this task every 24 hours?  I am running off windows now, but will probably switch to linux once I am live (if that makes a difference). I am working inside symfomy framework now.  I hear cronjobs can do this this...but looking at the site it seems to work remotely and I would rather just keep things in house...Can i just "run a service" on my computer? whatever that means ;) (have heard it used)  thanks for any help, Andrew
96825
Say I have the following script:  #!/bin/bash for i in $(seq 1000) do     cp /etc/passwd tmp     cat tmp | head -1 | head -1 | head -1 &gt; tmp  #this is the key line     cat tmp done   On the key line, I read and write the same file tmp which sometimes fails.  (I read it is because of race conditions because the processes in the pipeline are executed in parallel, which I do not understand why - each head needs to take the data from the previous one, doesn't it? This is NOT my main question, but you can answer it as well.)  When I run the script, it outputs about 200 lines. Is there any way I can force this script to output always 0 lines (so the I/O redirection to tmp is always prepared first and so the data is always destroyed)? To be clear, I mean changing the system settings, not this script.  Thanks for your ideas.
83950
I'm confused about the difference or advantage (if any) of running a set of tasks in a .sh script using GNU parallel  E.g. 's answer:  parallel ./pngout -s0 {} R{} ::: *.png   rather than say looping through them putting them in the background &amp;.  E.g. 's answer:  #copied from the link for illustration for stuff in things do ( something   with   stuff ) &amp; done wait # for all the something with stuff   In short are they just syntactically or practically different? And if practically different when should I use each?
79110
I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, I tried writing one but the script continues pinging until the count ends,  Plus I need to put that process in the background but if I run the script with the dollar ($) sign it still runs in foreground,  #!/bin/bash ping -c30 -i3 192.168.137.163 if [ $? -eq 0 ] then /root/scripts/test1.sh exit 0 else echo “fail” fi
36860
I am trying to run a bash script I have via cron, and I am getting the following error at the beginning of the execution:  tput: No value for $TERM and no -T specified   Here is what is in my crontab:  0 8 * * 1-5 cd /var/www/inv/ &amp;&amp; /var/www/inv/unitTest run all 2&gt;&amp;1| mail -r "user@example.com" -s "Daily Inventory Unit Test Results" user@example.com
76879
I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution.  I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)
55288
G'day,   I have tried to solve this problem all morning but have not had success. I would like to search for certain scripts in a particular folder and execute them all in parallel. The scripts have different names, but all end with "run.sh". Below what I have tried without success.   The first approach was to use find in combination with -execdir. This works, however, the scripts are executed in a sequantial fashion. I want all scripts to be executed simultaneously (parallel). There seems to be no option to achieve this with -execdir  find . -name "*run.sh" -type f -execdir 'nohup' {} '&amp;' \;   Then, I tried using xargs because there is has a parallel option (-P). I have not tried the parallel option yet, because I am unable to get xargs to run the scripts in their respective subfolders. The commands below execute all scripts, but do that in the folder I am running the command in, therefore the scripts themselves do not work. The scripts have to be executed in their own subfolders. In the above example, -execdir is doing that as opposed to -exec. How do I achieve that with xargs?  nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c f\ \{\}   or  nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c "f {}"   or  nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c "./{}"    I am really frustrated and am hoping that there is somebody out there that can help.  Thank you so much!
115679
Am I correct to assume that when ; joins two commands on a line, Bash always waits until the first command has exited before executing the second command? And similarly, in a shell script containing two different commands on different lines, Bash always waits until the command on the first line has exited before executing the command on the second line?  If this is the case, is there a way to execute two commands in one line or in a script, so that the second command doesn't wait until the first command has finished?  Also, are different lines in a shell script equivalent to separate lines joined by ; or &amp;&amp;?
92759
I'm trying to ensure a script remains running on a development server. It collates stats and provides a web service so it's supposed to persist, yet a few times a day, it dies off for unknown reasons. When we notice we just launch it again, but it's a pain in the rear and some users don't have permission (or the knowhow) to launch it up.  The programmer in me wants to spend a few hours getting to the bottom of the problem but the busy person in me thinks there must be an easy way to detect if an app is not running, and launch it again.  I know I could cron-script ps through grep:  ps -A | grep appname   But again, that's another hour of my life wasted on doing something that must already exist... Is there not a pre-made app that I can pass an executable (optionally with arguments) and that will keep a process running indefinitely?  In case it makes any difference, it's Ubuntu.
11929
I often start a long running command that is bound to either CPU, Disk, RAM or Internet connection. While that is running, I find that I want to run a similar command. Let's say downloading something big.  Then I start a shell with wget ...1 and let it run. I could open another shell and run the other wget ...2, but now they would fight for the bandwidth. When I just type the second command into the running shell, it will execute that later on, since wget is not interactive.  Is there a reasonable way to do this with either Bash or Fish Shell?
99603
I have a script that needs to be run every five seconds. I know that cron can do tasks by the minute, but is there a way to run something every second?