How to run a scrip if the time is matched with a time in another time zones - linux

Currently, I am using cloud VMs to run my code and because of that I am assigned with a new VM that is in a different time zone. I want to run a bash script that runs a python script at 7:30 pm (Eastern time). From here I know how to run a bash script at a specific time, e.g., echo "ls -l" | at 07:00. From here I know how to get the current time of Eastern time, e.g., TZ=America/New_York date. Also, from here I know how to get only the time using date +%R.
I am a Python coder and tried my best to write a sudo code that shows what I am trying to accomplish as a bash script:
while true
do
Now=TZ=America/New_York date +%R
if [Now -eq 7:30pm]
then
python3 myfile.py
done

As you already know how to set the at command to execute a command
at the specified time, and how to convert the EST to the local time,
you can just combine them:
echo "python3 myfile.py" | at $(date -d "19:30 EST" +%R)
When you invoke the at command, it always warns
"commands will be executed using /bin/sh". It will matter only if we invoke
a bash specific command such as:
echo "shopt -s globstar; ls **" | at ..
which will fail.
In our case, the command python3 myfile.py will run with both
/bin/sh and /bin/bash then you do not worry about the warning.
date -d STRING interprets the STRING as a date/time representation
and prints the converted date/time in the specified format +%R.
If you want to send the output to a file, you can say:
echo "python3 myfile.py > /path/to/a/file" | at $(date -d "19:30 EST" +%R)
In order to output to the current terminal, first identify the terminal
with tty command:
$ tty
=> /dev/pts/0
Then redirect the output to the terminal with:
echo "python3 myfile.py > /dev/pts/0" | at $(date -d "19:30 EST" +%R)

Related

take runDate of cron job as an input to the script which is being run

I have written a cronjob which runs on daily basis.
#!/bin/sh
Z=$(cat /home/saurabh/scripts/2017-09-15)
echo "$Z"
Y="File Content $Z ,Done"
echo "$Y"
I have made an entry in crontab file to run this script on daily basis.
I want to take 2017-09-15 as a variable depending upon the date on which cron job is running. How can I do that ?
You can get the present date at the time of the cron run in your
specified format with date -I. Right now, that matches the string
in your Z.
#! /bin/sh
# Optionally put into temp var.
dt=$(date -I)
Z=$(cat /home/saurabh/scripts/$(date -I))
# or: Z=$(cat /home/saurabh/scripts/$dt)
echo "$Z"
...

How to get time in seconds since process started, without using etimes?

Trying to use ps to find the number of seconds since a process started. The version of ps I have doesn't support etimes and not sure how else to get this info?
For process of pid 1234, you could use the mtime field of meta-data of pseudo-file /proc/1234/status. Read proc(5) for more.
See also stat(2) and stat(1) and date(1).
So date +%s is giving the current date since the Unix epoch, e.g. 1479125355 now in November 14th, 2016. stat -c %Y /proc/1234/status is giving the start time of process 1234 since Unix epoch. You want the difference. Perhaps use (barely tested, my interactive shell is zsh) $[$(date +%s) - $(stat -c %Y /proc/1234/status)]; adapt that to your shell.
For example:
bash -c 'sleep 4; echo $(($(date +%s) - $(stat -c %Y /proc/$$/status)))'
is giving me 4 as expected. Of course the $$ is expanded to the pid of the bash -c command

Bash variable expansion can't be combined with time command

I was writing a small shell script in bash to measure the speed of several executables using the time command. Because I wanted to time several different programs, to avoid repeating myself I saved the time command and the format string I wanted and tried to use variable expansion (testing the performance of ls as an example):
#!/usr/bin/env bash
timer="/usr/bin/time -f 'elapsed time: %E' --"
$timer ls 1>/dev/null
The time command is supposed to use the -- to separate its options (in this case a format string) from the command it's supposed to time. However, in this script time insists on trying to execute my format string and fails with
/usr/bin/time cannot run time:: No such file or directory
Command exited with non-zero status 127
'elapsed
But if I put the whole command on one line with no variable expansion it correctly recognizes the format string and executes ls:
#!/usr/bin/env bash
/usr/bin/time -f 'elapsed time: %E' -- ls 1>/dev/null
produces "elapsed time: 0:00.00"
I ended up just typing out the whole command on each line to get it to run, but I was curious if anyone could explain why variable expansion doesn't work here.
The data in $timer is split on $IFS (spaces), and used as separate arguments. It is not expanded and then evaluated as bash code.
Your command $timer ls is therefore equivalent to:
"/usr/bin/time" "-f" "'elapsed time:" "%E'" "--" "ls"
The correct way to do this in your case is using a function:
timer() {
/usr/bin/time -f 'elapsed time: %E' -- "$#"
}
timer ls
PS: This problem is automatically pointed out by shellcheck.
You need to use a BASH array to store command line:
timer=(/usr/bin/time -f 'elapsed time: %E' --)
"${timer[#]}" ls

Why doesn't "echo" show up in "ps"?

I am having great difficulty in understanding what shows up on ps command. To test my understanding I created below dummy script
#!/bin/bash
for i in {1..100000}
do
date -u
date -u
date -u
done
while running this script I opened a new terminal and executed repeatedly
ps -eaf | grep date | grep -v grep
and I was able to date process in the output.
I later changed dummy script by replacing date -u with echo "what is going on"
#!/bin/bash
for i in {1..100000}
do
echo "What is going on"
echo "What is going on"
echo "What is going on"
done
while running the updated dummy script, I opened a new terminal and executed repeatedly
ps -eaf | grep echo | grep -v grep
and echo was never shown in output. Why is this? I suspect the reason is the script being a bash script, may be it is using builtin echo therefore it was not displayed in ps output. Am I correct? What am I missing here?
echo is a builtin in bash:
$ type echo
echo is a shell builtin
That means that a new process is not created when echo is run. All the work is done by the bash process instead, which is way more efficient.
You can run the non-builtin echo explicitly:
command echo "What is going on"
This forks and execs /bin/echo instead, letting it show up in ps.

Run random command in bash script

I would like to randomly select one random command in a bash script.
I have tried commands like
echo $(( $RANDOM % 12 ))
But they work in the UNIX command line but not in my bash script.
Anyone have a solution for my problem?
-- Updated answer as per discussion in comments --
For Bash Users:
Code given in question works fine. Just don't forget to set permissions to +x (grants executable permission to all). Eg: chmod +x myscript.sh
Caution: Don't set RANDOM to a value. It looses its special properties.
Bash man page says-
RANDOM Each time this parameter is referenced, a random integer
between 0 and 32767 is generated. The sequence of random numbers may
be initialized by assigning a value to RANDOM. If RANDOM is unset, it
loses its special properties, even if it is sub- sequently reset.
For dash Users:
sh in ubuntu is a symbolic link to dash.
dash does not support RANDOM variable, it is just another variable.
If you are bound to use sh, you can use date function to generate
pseudo randoms only if your script is not time-dependent. (if your
script runs at strict intervals of time, it might produce same values)
RANDOM=`date '+%s'`
echo $(( $RANDOM % 12 )) # To generate random numbers less than 12
In General:
If you want to generate true random numbers use this-
dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" "
Source: http://www.linuxquestions.org/questions/programming-9/shell-script-random-variable-4088/
Create the shell script test.sh
chmod +x test.sh
content of test.sh
#!/bin/bash
echo $(( $RANDOM % 12 ))
Run it via
bash test.sh
or
./test.sh

Resources