is there a way to temporarily reset the system date (Mountain Lion) with a script?
My idea would be of a script that reset the system to a chosen date/time (and disable the automatic synchronization).
And, maybe, a second script that re-select the checkbox 'set date and time automatically' ( but this of course can be easily done manually…)
Use the -setdate or -settime options for systemsetup:
systemsetup -setusingnetworktime off -setdate 12:31:2013 -settime 23:59:59
Without -setusingnetworktime off there was an error like this:
setDate: Error, you cannot set the date while network time is running. Use '-setusingnetworktime' to turn network time off.
This restores setting date and time automatically:
systemsetup -setusingnetworktime on
Related
I want to print the date after every bash command I run.
This could help me understand how much a command took to execute when I am away from keyboard.
I know I could do
`DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`
to get the date but I don't know how or even if it could be possible to run this command after every command I execute on bash.
I would also be interested in running the same command before every command so I could know how long a command took.
Is it possible?
What file should I edit?
For example:
$ wget google.com
15/07/2017 23:40:05
I would be happy, if I could also introduce this following feauture:
$ wget google.com
15/07/2017 23:40:05 15/07/2017 23:40:11
Program run for 00:00:06
where the first date is when I ran the program, second is when program terminated the third is self-explanatonary.
As you understood, I don't want to type every time
$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`
To execute a cmd before every command entered, set a trap on DEBUG. Eg.
trap date DEBUG
To execute that command before emitting a prompt, set PROMPT_COMMAND:
PROMPT_COMMAND=date
This does exactly that:
PROMPT_COMMAND+=$'\n'"date +%d/%m/%Y\ %H:%M:%S"
The string in PROMPT_COMMAND gets evaluated after every command. You just need to add the date command to whatever you already had in it. ($'\n' (newline) is a somewhat more robust joiner than ; as two consecutive ; would give you a syntax error)
You can add date/time to your prompt, via PS1 variable. You could use date command, but it's more efficient to use the supported special characters, like \d for date, or \D{strftime-fmt}.
For example:
PS1='\u#\h[\D{%F} \D{%T}]\w\$ '
or, with color:
PS1='\[\033[01;32m\]\u#\h\[\033[00m\][\[\033[02;33m\]\D{%F}\[\033[08m\]T\[\033[00m\]\[\033[02;33m\]\D{%T}\[\033[00m\]]\[\033[01;34m\]\w\[\033[00m\]\$ '
will show:
user#host[2017-07-16 00:01:17]~/somedir$
Note that in the second case (with color) we have a valid ISO8601 timestamp, with a "hidden" date/time separator T in the middle. If you select it with a mouse, T is visible and can be copied. (Also double-click will select the complete timestamp, not only date or time.)
To print timestamp after every command just modify your PS1 prompt and add date to it. The only catch here is that it will tell you time when command ended and new prompt showed. So in case you have your prompt open for long time just hit enter to capture start time before running your command.
PS1="\D{%F %T} \$ "
See this arch wiki page or just google bash prompt customization.
To add time spent executing program just add time before the command
$ time wget google.com
It will give you output like this
real 0m0.177s
user 0m0.156s
sys 0m0.020s
And you can get even more lazy and for commands that you dont't feel like typing time every time you run it, just create alias.
alias wget="time wget"
Because in bash aliases are run before other commands you can do it this way even if it looks like recursion. Then you will call it as you are used to.
And of course, aliases and prompt settings can be put in your .bashrc file, so you don't have to type them every time you open terminal.
I need to create some sort of fail safe in one of my scripts, to prevent it from being re-executed immediately after failure. Typically when a script fails, our support team reruns the script using a 3rd party tool. Which is usually ok, but it should not happen for this particular script.
I was going to echo out a time-stamp into the log, and then make a condition to see if the current time-stamp is at least 2 hrs greater than the one in the log. If so, the script will exit itself. I'm sure this idea will work. However, this got me curious to see if there is a way to pull in the last run time of the script from the system itself? Or if there is an alternate method of preventing the script from being immediately rerun.
It's a SunOS Unix system, using the Ksh Shell.
Just do it, as you proposed, save the date >some file and check it at the script start. You can:
check the last line (as an date string itself)
or the last modification time of the file (e.g. when the last date command modified the somefile
Other common method is create one specified lock-file, or pid-file such /var/run/script.pid, Its content is usually the PID (and hostname, if needed) of the process what created it. Of course, the file-modification time tell you when it is created, by its content you can check the running PID. If the PID doesn't exists, (e.g. pre process is died) and the file modification time is older as X minutes, you can start the script again.
This method is good mainly because you can use simply the cron + some script_starter.sh what will periodically check the script running status and restart it when needed.
If you want use system resources (and have root access) you can use the accton + lastcomm.
I don't know SunOS but probably knows those programs. The accton starts the system-wide accounting of all programs, (needs to be root) and the lastcomm command_name | tail -n 1 shows when the command_name is executed last time.
Check the man lastcomm for the command line switches.
I have some prompt set in oh-my-zsh theme which include some 'curl/wget' commands which obtain weather status. But every time I start the shell, prompt just waiting like 9-10 seconds before welcome me. What I'd like to do is to be able to set 'dynamic' prompt: default prompt without weather shows up momentarily when I start shell, then in 'background' weather is obtained and added to prompt (for example, when some command was executed and another one prompt shown). How can I do that?
UPDATE: I decided to go with cron job which will fetch weather simply every 5 minutes and then I can cat it not only to zsh prompt, but to any system part I want (for example, to WM statusbar). Although for people who are looking for answer on my exact question I'd recommend to try something like James Andrews proposed.
In your .zshrc file you could set
# How long to wait before calling TRAPALRM()
TMOUT=300
# called when TMOUT reaches 0
TRAPALRM()
{
# run this command in the background so my shell resets
{
export WEATHER=$(...)
}&!
}
And your prompt could use the $WEATHER variable.
Couple of ideas:
use the curl --max-time option to limit the damage.
have a cron job that runs every 5 minutes or so, fetch and write the weather to a file, your prompt can read the file.
The .bash_history file is a life-save for many of us. Unfortunately, BASH only seems to save the commands of a session when that session is closed (via exit).
It's a tragedy, then, when all your commands from an important session are vaporized when a session is closed unexpectedly -- before it gets to archive all the commands with fancy syntax that took hours to get right....
This happens to me when I forget to close a SSH connection when leaving work, and it gets disconnected due to inactivity (Write failed: broken pipe), or when I restart my computer without closing my terminals manually, and so on.
I would love to have my BASH commands archived after some interval -- say every 10 minutes -- so that if I do close a session, my commands will still be there. This seems like something a lot of people might find useful.
Does anyone have an idea of how to do this?
Ideally....
The functionality would require no extra effort on the user's part once set up -- something he/she could add to ~/.bashrc
The user could change the backup interval
It would avoid using temporary files, aliasing bash, or other "hacks"
StackOverflow-ers -- consider yourself challenged!
You can use history command with -a option:
history
-a Append the ``new'' history lines (history lines entered since the
beginning of the current bash session) to the history file.
You can write each and every command to history file at once with a little help of PROMPT_COMMAND function:
PROMPT_COMMAND
If set, the value is executed as a command prior to issuing each primary prompt.
So just put this into .bashrc
PROMPT_COMMAND="history -a"
According to this bash will (usually) receive a SIGHUP on disconnect.
Using trap we can write the history (lame as #*?! that bash doesn't do it by default though..):
function rescue_history {
history -a
}
trap rescue_history SIGHUP
Put the above in your .bashrc
I need a bash script that sets a date for my program. The program must work with a date different from the current one. Is it possible? With:
#!/bin/sh
date 122511462014.30 && myprogram
I get the following message error
date: cannot set date: Operation not permitted
because my script runs with no root privileges.
You can't do it like that. date changes the date for the entire system.
You need something like libfaketime