Bash scripting with skeleton in Debian [duplicate] - linux

This question already has answers here:
Cron jobs -- to run every 5 seconds
(6 answers)
Closed 8 years ago.
Before of first, I'm a Linux (administrator|developer) newbie.
I need to run a bash script every 5 seconds, it's very simple; export service's information to text files.
I try to do this with cron daemon, but it's run every minute at least.
I'm discover Skeleton script and have many questions about this:
I need write some special code in my bash file?
How to run every 5 seconds?
There are a best practices manual?

Yes its not possible through cron as daemon runs once in every minute. Or when job list is modified
Put whatever you want to run in a script inside infinite while loop and put sleep of 1 sec
something like
while [ 1 ]
do
run_your_cmds here
sleep 1
done
BUT I dont think anything need that kind of monitoring.
Best Practice!!
Please dont try and do it with cron.

Related

Increment Counter in a while loop after X seconds [duplicate]

This question already has answers here:
Command line command to auto-kill a command after a certain amount of time
(15 answers)
Closed 3 years ago.
I am trying to run a script which takes input from text file and based on the number of entries in it, a command is executed as many number of times.
Below is an overview:
cat /tmp/file.txt | while read name
do
<<execute a command using value of $name>>
done
What is happening is sometimes the command executed for particular $name is getting hung due to known issues. Therefore I need in such cases that the command on every value of $name runs only for X number of seconds and if it is not able to complete within that stipulated time, terminate the process and increment loop counter.
I was able to make use of sleep and kill but it is terminated the entire loop. I want the next values to be processed in case command gets hung on a row/value.
Please advise.
Sounds like you might want something like timeout.
timeout 4 <command>

Execute a command when a bash script is not responding? [duplicate]

This question already has answers here:
How to kill a child process after a given timeout in Bash?
(9 answers)
simple timeout on I/O for command for linux
(3 answers)
Closed 5 years ago.
Here's my situation: I've made a script in a while loop, but sometimes (say after 20-30 loops) it stops unexpectedly.
I tried to debug it but I couldn't.
I noticed that it stops while executing a command, and it just doesn't do anything when it stops. Now I was thinking: is there a way to tell to another script when the first script stops and it doesn't execute any command in, say 120 seconds? Maybe by constantly observing the output of the first script and when it's giving no output, the second script kills the first one and makes it start again? Sorry for my bad English hope I was clear.

Is there a way to run a program and kill every 20 seconds in linux?

I have a program that i need to collect 300 pieces of data from, but to manually do the collecting i have to run the program on my ubuntu virtual machine and record the data on excel. It takes a long time to do this whole process. I was wondering if there was a command in linux that i could use to call commands make and to kill me program.
I search watch and tried it but it doesnt work for me:
watch -n 20 make play
where make play runs my program
Yet this doesnt fo everything i want to do. I want to do this every 20 seconds so i have enough time to write my data to my excel file
1. make play (run my program so it prints what i need to record)
2. kill my program
Is there a command for this?
I think you should rethink what you are doing - I can't think of a setting where running and killing a program every 20 seconds makes any sense.
That being said, the standard way to run programs periodically in linux is a cron job. Cron has a 1 minute minimum though, so you would have to write a script that starts 3 instances of your program with 20 second delay, and run this script with cron every minute. You can combine this with the timeout utility, which will kill your program if it is still running after a given time. A quick google search should provide you with further details.
I think you Could use crontab, man crontab to get the manual of crontab. However, you may not be able to run and kill every 20s, at least every 1 min. Hope It could help.

Calling a specific method on a certain time

I am currently working on a web application and I want to schedule a method that runs every day at 6 AM. Basically, if the clock ticks at 6 AM, this method will run. I have seen some answers on the Internet but none of these are scheduled at a fixed time of the day but rather timed after the app is ran.
I also have an alternative solution which is to check every minute if the time is 6 AM, and if so, I'll run the code. But is there other better answer than this?
Thank you!
On the server side, you could do it with a cron job.
On Linux follow these steps:
crontab -e
Here's an example of how to make a request to the google.com at 6AM (UTC)
# Each day at 6th hour (6 AM depending on your time zone settings, or UTC)
0 6 * * * curl -I http://google.com
:wq # save changes and quit
:q # to quit
:q! # to quit without saving changes
See more about Cron here. Or use a visual crontab entry creator if that is helpful. If you need a solution for Windows machine, you could also use a Windows scheduler to trigger events similar to cron.
Cron job would be a good way of doing it. However, for some reason if you can not schedule a cron job and need your own scheduler and your environment is Java based then you could use the Quartz Scheduler. It is a java based job scheduler like cron. It's usage is pretty simple.

Multitasking in a bashscript [duplicate]

This question already has answers here:
Multithreading in Bash [duplicate]
(3 answers)
Closed 9 years ago.
I am basically trying to write a bash script that suspends some virtual machines running on a host. However, if I write the script sequentially, VMs will be suspended one at a time. Suspending a VM takes some time to save state. How can I let my script suspend the VMs concurrently. In other words, how can I run commands concurrently in a bash script instead of sequentially?
You can background the tasks.
some bash command with options and stuff then with a &
Adding the & will send the command to the background and begin the next.
Put a & after the command for suspending the the VM.
For example if
cmd_to_suspend_vm
was your command to run. You would run
cmd_to_suspend_vm &

Resources