How to set time in Linux by Bash that not change after reboot? - linux

I'm trying to code a bash script that set Time and Date manually, this work but after reboot my system (Ubuntu 18.04) time and date resets to automatic mode. How I can write a bash for set time/date that not change after reboot?
My try as a beginner ended to this Bash script:
#!/bin/bash
# A simple bash script to set system date & time
timedatectl set-ntp 0
date +%Y%m%d -s $1
date +%T -s $2":00"

timedatectl set-ntp 0 && timedatectl set-time 'HH:MM:SS' will turn off web clock sync and let you update it manually.

I done it by using Hard Ware Clock.

Related

Regarding cronjob execution time

I am having list of cron job functions.Each function run for certain period of time.Is there any way to find how long each cron function execute,Like time of start executing function and end time?
Expected output:
Cron tab start time: timestamp
cront tab end time: timestamp
Read crontab(5). A cron job is just a shell command or script. That shell could, for example, use logger(1) mixed with date(1) and time(1). See also time(7). See also GNU bash documentation.
A cron job could also be a GUILE script, or a Python one. See execve(2), fork(2) and other syscalls(2) which is called by crond
And cron is open source, so you are allowed to study its source code.... (e.g. of GNU mcron). Or use strace(1) on it to understand its behavior.
Consider reading Advanced Linux Programming and see Linux From Scratch.
For example, I am running thru cron a backup script starting with
#!/bin/bash
cd $HOME
renice -n +2 -p $$
ionice -c Best-effort -n +5 -p $$
tm=$(date +"backup-home+%Y-%b-%d-%Hh%M")
logger --id=$$ -t backup-home start $tm
sync

How to check last running time of any script in linux instance

I want to check if my scripts ran the last night(or last ran timestamp) on linux instance based on scripts crontab running time stamp.
So how to get scripts last ran time on linux instance?
I would suggest better record the start time during the start of the script and end time at the end of the Script.
# Start Time Entry
echo "Start : " $(date +%T) > exec.log
start=`date +%s`
CALL YOUR SCRIPT HERE
# End Time Entry
end=`date +%s`
echo "End : " $(date +%T) >> exec.log
# Get the Runtime
runtime=$((end-start))
echo "Runtime: $runtime" >> exec.log
If there is any better way, I am also curious to see and implement too.
grep cron from your "messages" or "syslog
grep -i cron /var/log/messages
or create a separate log file for cron from rsyslog, edit file /etc/rsyslog.conf and change #cron to cron. You will find logs in /var/log/cron

Issue with time command

When I run several background jobs, I need to get a runtime for the set of jobs.
I use the following example successfully:
$ sleep 10 &
$ time wait
real 0m9.74s
user 0m0.00s
sys 0m0.00s
However, if I try to format the output to seconds, I get the following error:
$ time -f %e wait
time: cannot run wait: No such file or directory
0.00
This only seems to happen with shell builtins. Is there a workaround for this?
The problem here is two-fold.
we don't know if you're using the bash (assuming you're using the centos default shell) built-in or the binary time command.
in either case wait is a bash built-in
Ways to deal with this:
/usr/bin/time -f %e bash -c wait
TIMEFORMAT="%pR" time bash -c wait

how to get time execution of a program in linux terminal separately

I want to get time of execution of a program in my terminal. I know that I should use this command:
time chmod +x ~/example
but the output is this:
real 0m0.088s
user 0m0.057s
sys 0m0.030s
But I want to access each one separately. for example just real. how can I get that?
You can use -f to format the time command:
$ /usr/bin/time -f "\t%E Elapsed Real Time" touch a
0:00.00 Elapsed Real Time
The geek stuff has a very broad documentation on this topic:
12 UNIX / Linux Time Command Output Format Option Examples.
It is also funny that calling it with time alone did not work to me, I have to use the full path.
In bash, you can influence the ouput of time with the TIMEFORMAT variable, by setting
TIMEFORMAT=%R # real
TIMEFORMAT=%U # user
TIMEFORMAT=%S # sys
before calling it. However, your problems probably don't end there -- capturing the output of time is not trivial with bash because it's not a subprocess but a shell builtin. There's an entry in the bash FAQ on the topic. Going from there, I think you ultimately want
TIMEFORMAT=%R myvar=$( { time chmod +x ~/example > /dev/null 2>&1; } 2>&1 )
Then $myvar will be the real running time of the command.
You can do:
(time chmod +x ~/example) |& awk '$1=="real"{print $2}'
0m0.003s

How can I trigger a delayed system shutdown from in a shell script?

On my private network I have a backup server, which runs a bacula backup every night. To save energy I use a cron job to wake the server, but I haven't found out, how to properly shut it down after the backup is done.
By the means of the bacula-director configuration I can call a script during the processing of the last backup job (i.e. the backup of the file catalog). I tried to use this script:
#!/bin/bash
# shutdown server in 10 minutes
#
# ps, 17.11.2013
bash -c "nohup /sbin/shutdown -h 10" &
exit 0
The script shuts down the server - but apparently it returns just during the shutdown,
and as a consequence that last backup job hangs just until the shutdown. How can I make the script to file the shutdown and return immediately?
Update: After an extensive search I came up with a (albeit pretty ugly) solution:
The script run by bacula looks like this:
#!/bin/bash
at -f /root/scripts/shutdown_now.sh now + 10 minutes
And the second script (shutdown_now.sh) looks like this:
#!/bin/bash
shutdown -h now
Actually I found no obvious method to add the required parameters of shutdown in the syntax of the 'at' command. Maybe someone can give me some advice here.
Depending on your backup server’s OS, the implementation of shutdown might behave differently. I have tested the following two solutions on Ubuntu 12.04 and they both worked for me:
As the root user I have created a shell script with the following content and called it in a bash shell:
shutdown -h 10 &
exit 0
The exit code of the script in the shell was correct (tested with echo $?). The shutdown was still in progress (tested with shutdown -c).
This bash function call in a second shell script worked equally well:
my_shutdown() {
shutdown -h 10
}
my_shutdown &
exit 0
No need to create a second BASH script to run the shutdown command. Just replace the following line in your backup script:
bash -c "nohup /sbin/shutdown -h 10" &
with this:
echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1
Feel free to adjust the time interval to suit your preference.
If you can become root: either log in as, or sudo -i this works (tested on ubuntu 14.04):
# shutdown -h 20:00 & //halts the machine at 8pm
No shell script needed. I can then log out, and log back in, and the process is still there. Interestingly, if I tried this with sudo in the command line, then when I log out, the process does go away!
BTW, just to note, that I also use this command to do occasional reboots after everyone has gone home.
# shutdown -r 20:00 & //re-boots the machine at 8pm

Resources