Bash: Program next execution of current script using 'at' - linux

I want to execute a script and make it schedule the next execution. A sample would be:
#!/bin/bash
TMP=/tmp/text.txt
SCRIPT=$(readlink -f $0)
date >>$TMP
at -f $SCRIPT now + 1 minutes >>$TMP 2>&1
echo -e "\n" >>$TMP
A sample execution would do as follows:
First execution OK. Schedules to next minute
Second execution writes OK but doesn't schedule
Resulting output would be:
tue mar 5 14:34:01 CET 2013
job 15 at 2013-03-05 14:35
tue mar 5 14:35:00 CET 2013
job 16 at 2013-03-05 14:36
[now at 2013-03-05 14:38]
atq outputs nothing and I don't see any /var/at/jobs (In fact, ls /var/at* outputs nothing. There is no message in any user in /var/mail/. I'm trying on a CentOS release 5.6 x86_64
Anyone has any hint as to what may be happening?

suspectus, you have hit the point... echo $SCRIPT gives '/bin/bash'... I've manually written the full path and now it works

Related

How to get the time when I last used the computer

In GNU/Linux on xorg session, what I want to do is to get how many seconds have passed since I stopped working with the computer (i.e. no keys pressed and/or cursor moved).
Running in the background, the script below will display secs in the status bar.
But the question is what THE_COMMAND will be.
While true; do
last_touched="$(THE_COMMAND)"
now="$(date +%s)"
secs=$((now - last_touched))
echo "${secs} seconds ago"
sleep 3
done
I remember asking the same question a while back.
Here is what I found,
last -aiF -n 1 userName
command can give you the current session.
When combined with awk you can get the result as follows
$ last -aiF -n2 username
username :1 Wed Apr 21 13:09:00 2021 still logged in 0.0.0.0
username :1 Wed Apr 21 07:28:47 2021 - down (05:39) 0.0.0.0
$ last -aiF -n 2 ogulcan | awk '{print $10}'
in
(05:39)
the lines here are the session times.
These times are counted as now - first boot login
But I believe these does not work best for you.
So here is the 8 year old question that may be helpful to you.
User Idle time in Linux
Using python you can calculate the idle time passed. Maybe this way, you can simply get what you want with python.

get average of times that take job finish in for loop shell script

Here is my first shell script
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10000 ]; do
date;
time wget -q 'http://exmple.com/' > /dev/null | grep real;
sleep 3;
let COUNTER=COUNTER+1
done
echo ${COUNTER} Request Sent\n
Average Response Time is :
this script download page content and calculate time of page response
i need to store Real parameter and calculate average of it
the output of script is something like that
Tue Oct 25 22:43:36
real 0m13.275s
user 0m0.004s
sys 0m0.008s
So my problems are :
How can i add some jobs (like echo ${COUNTER} ) after pressing crtl+c (stopping script)
How can i store value of "real" in seconds // sometimes page response goes to minutes , so basically need some time convert function (?)
i tried to solve the problems but as i mentioned i am training shell script
#!/bin/bash
echo "How many runs would you like to perform?"
read limit
total_time=0
counter=1
while [ ${counter} -le ${limit} ]
do
date
run_time=`(time -p wget -qO- http://exmple.com/) 2>&1 > /dev/null | grep real | awk '{print $2}'`
echo "Run ${counter} completed in ${run_time} seconds"
total_time=$(bc<<<"${total_time}+${run_time}")
if [ ${counter} -ne ${limit} ]
then
sleep 3
let counter=counter+1
fi
done
avg=$(printf "%.3f" "$(bc -l <<<"${total_time}/${limit}")")
echo "${counter} requests were sent taking ${total_time} seconds"
echo "Average response time was : ${avg} seconds"
Above is a modified script to perform the operations you requested, with a couple modifications
Added a question then input to ask how many runs to make each time, instead of a static number
Print out total time for runs and then average per run in 3 decimal places
Skip sleeping if it's the last run
You'll notice we use bc instead of let for a couple math lines. The bc command allows the use of decimal places.
https://www.gnu.org/software/bc/manual/html_mono/bc.html
To get the value in seconds I had to use awk to separate the value from the title 'real' in the line. Then you can do the math on the number value.
One other thing I noticed in your initial script. You used both ways of displaying a variable:
$COUNTER and ${COUNTER}
While both are legitimate, I got into the habit of always encapsulating my variables, it makes things easier when you want to add to the end of it.
For example let's say I have a number defined (NUM=7), but want to display it times 10. If I don't encapsulate echo $NUM0 then I get an error because the variable $NUM0 wasn't defined. But if I encapsulate echo ${NUM}0 will display an output of 70.
All of this results in a final output as below
How many runs would you like to perform?
5
Tue Oct 25 16:02:58 MST 2016
Run 1 completed in 0.09 seconds
Tue Oct 25 16:03:01 MST 2016
Run 2 completed in 0.08 seconds
Tue Oct 25 16:03:05 MST 2016
Run 3 completed in 0.07 seconds
Tue Oct 25 16:03:08 MST 2016
Run 4 completed in 0.09 seconds
Tue Oct 25 16:03:11 MST 2016
Run 5 completed in 0.08 seconds
5 requests sent taking .41 seconds
Average response time was : 0.08 seconds
Please let me know if you have any other questions about the script.
You can record a reasonably precise time with e.g.
t1=$(date +%s.%N)
wget ...
t2=$(date +%s.%N)
diff=$(($t2-$t1))
For doing stuff after Ctrl+C, which is really a SIGINT signal, read up about the trap builtin.

inconsistent results from shell script mv

I'm having an issue with a simple shell script that merely moves a file from one location to another. It takes the source & target paths & names as parameters. Code is as follows...
#!/bin/sh
if [ ! -f $1 ]; then
exit 1
else
ls -l $1
mv -v $1 $2
X=$?
echo 'Move completion code = ' $X
ls -l $2
exit $X
fi
This script is called multiple times in a sequential job schedule, loading data to (replacing) the same file. Recently seeing instances where the job after the move script runs with data from the file that existed before - indicating that it was not overwritten - yet the messages in the move script show a successful move - and checking the file itself shows it with the expected data. Any ideas? Could the script be completing before the mv command has fully completed? I've put the completion code message in to check for this, I've also tried sleeping for 5 sec. What am I missing?
Thanks!
Update...
Here's the output after adding the md5sum checks....
-rw-r--r--. 1 appworx dba 22 Aug 10 12:15 UP.dat
Before move - file 1
9bf394b473cf389d704357e4da46fcfa UP.dat
`UP.dat' -> `ids.dat'
removed `UP.dat'
After move - file 2
9bf394b473cf389d704357e4da46fcfa
Move completion code = 0
-rw-r--r--. 1 appworx dba 22 Aug 10 12:15 load_ids.dat

Cron executing wrong

was set on crontab the line below. But is not validate the day. When I remove de day of week is executed correctly. Any suggestion?
# uname -a
Linux server 2.6.32-358.14.1.el6.x86_64 #1 SMP Tue Jul 16 23:51:20 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
# cat /etc/issue
Red Hat Enterprise Linux Server release 6.4 (Santiago)
Kernel \r on an \m
# date
Mon Mar 2 08:50:19 BRT 2015
# crontab -l
* * 1 3 1 echo "teste"
# tail -f /var/log/cron
Mar 2 08:38:01 server CROND[10509]: (root) CMD (echo "teste")
If the time interval can't be defined in crontab, you can use a date/time check in the script itself. You can force crontab to run script at every 2nd March and check inside the script if the day is actually Monday. If not, you can exit the script.
Eg.
[[ $( date +%u ) -ne 1 ]] && exit
If the day isn't Monday, exit.
About crontab
"... there is one exception: if both "day of month" and "day of week" are restricted (not "*"), then either the "day of month" field (3) or the "day of week" field (5) must match the current day. ..."
A similar problem is said in Run a cron job on the first Monday of every month? and How to run a cron job on the first weekday of the month
So, in crontab set:
* * 1 3 * [ "$(date '+\%a')" == "Sun" ] && /bin/mkdir /tmp/cronsilvioteste

crontab job skipping to run randomly

I have a crontab job setup to run every 5 minutes. it runs fine without any issues. But once a while a run fails.
e.g
its run fine at 5th, 10th , 15th minute of an hour but the 20th minute will not run but runs fine again at 25th minute.
next time may be the 55th minute fails. ( random times on random servers)
i checked in /var/log/cron and there are entries for all the executed jobs but for the missed job there is not even an entry.
eg. in the below /var/log/cron job rmlogs ran fine at 21.35 missed running at 21.40 but runs again at 21.45.
Jun 12 21:35:01 [serverxxxx] CROND[4167]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:40:01 [serverxxxx] CROND[4201]: (root) CMD (/usr/lib64/sa/sa1 -S DISK -F 1 1)
Jun 12 21:41:02 [serverxxxx] CROND[4213]: (root) CMD (/usr/local/bin/monitor_mcollective >/dev/null 2>&1 #PUPPET)
Jun 12 21:45:01 [serverxxxx] CROND[4225]: (riak) CMD (/opt/riaktools/riak-create-logs -p /var/log/riak > /dev/null 2>&1 #PUPPET)
Jun 12 21:45:01 [serverxxxx] CROND[4227]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:50:01 [serverxxxx] CROND[4665]: (root) CMD (/usr/lib64/sa/sa1 -S DISK -F 1 1)
Jun 12 21:50:01 [serverxxxx] CROND[4666]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:52:01 [serverxxxx] CROND[4700]: (root) CMD (/usr/local/bin/refresh-mcollective-metadata #PUPPET)
You have a reason to doubt, if your cron job has been started every 5 minutes indeed.
To exclude the doubt you can extend the crontab line, containing your cron job with a prefix:
date >>/tmp/base_name_of_your_cronjob.log;
and check this additional log file.
If this log file contains entries every 5 minutes without a gap, then you should investigate, if /var/cron/log gets a new record for every start of the cron job, including cases, if the cron job crashes.
Additional check point is to make sure that the cron daemon has run permanently and was not restarted. You can do it by checking its process by e.g.
ps -ef|grep crond

Resources