Debug, Unix. Run a program with a different time from the system - unix

I am debugging a program in MacOSX,
and I need that this program thinks we are one year later than the one given by the operating system.
I cannot change the time of the operation system, because I need to run a second program concurrently with the correct time.
I could modify the code of the first program to add one year each time it gets the time from the operation system, but the code is too big to do that; I prefer not to use this solution.
I heard once that there is a command in Unix to run a program with a fake/mocked time.
Do you know about it?

I haven't tried it, but libfaketime claims to do what you need.
Quoted from the website:
As an example, we want the "date" command to report our faked time. To do so, we could use the following command line:
user#host> date
Tue Nov 23 12:01:05 CEST 2007
user#host> LD_PRELOAD=/usr/local/lib/libfaketime.so.1 FAKETIME="-15d" date
Mon Nov 8 12:01:12 CEST 2007
Assuming the lib works as advertised, you can trick your program into thinking it was running a year ahead using:
LD_PRELOAD=/usr/local/lib/libfaketime.so.1 FAKETIME="+1y" ./your_program

Related

Running multiple xdtool commands from activateResult

I'm creating a gnome shell extension and implementing the search provider. In the activateResult method I want to run some code like
GLib.spawn_command_line_sync('xdotool windowactivate ' + window_id);
GLib.spawn_command_line_sync('xdotool key "ctrl+r"');
GLib.spawn_command_line_sync('xdotool type ' + some_text);
The problem is that only the first command works, and I get some errors like:
Jul 27 20:05:09 comp org.gnome.Shell.desktop[3334]: Window manager warning: Received a NET_CURRENT_DESKTOP message from a broken (outdated) client who sent a 0 timestamp
Jul 27 20:05:09 comp org.gnome.Shell.desktop[3334]: Window manager warning: Buggy client sent a _NET_ACTIVE_WINDOW message with a timestamp of 0 for 0x2e00001 (somestuff)
Jul 27 20:05:09 comp org.gnome.Shell.desktop[3334]: Window manager warning: last_focus_time (93207838) is greater than comparison timestamp (93207584). This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW. Trying to work around...
Jul 27 20:05:09 comp org.gnome.Shell.desktop[3334]: Window manager warning: last_user_time (93207838) is greater than comparison timestamp (93207584). This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW. Trying to work around...
Jul 27 20:05:09 comp org.gnome.Shell.desktop[3334]: Window manager warning: 0x2e00001 (somestuff) appears to be one of the offending windows with a timestamp of 93207838. Working around..
One thing I tried was to combine all the xdotool commands with bash -c "... ... ..." with no luck.
After selecting a search result, how can I switch to a window and simulate key presses?
(I'm brand new to gnome stuff, gjs stuff, and even JS, but, do write python daily)
edit: Just tried spawn_command_line_async and it works. Feels sloppy, someone with more experienced might have a better answer.
If you are writing a GNOME Shell extension, it's worth noticing that there are a number of GNOME platform libraries that you can rely on being available.
For simulating mouse and keyboard event, Atspi will work in most situations. As a simple example of simulating pressing the a key:
const Atspi = imports.gi.Atspi;
Atspi.generate_keyboard_event(0, 'a', Atspi.KeySynthType.STRING);
Activating a window, on the other hand, is probably something best done with the built-in window manager functions, however these aren't documented and you'd have to inspect the GNOME Shell JavaScript source or the source of another extension that does this.

How to run multiple julia files parallel?

I have a pretty simple question but I can't seem to find an answer anywhere. I want to run two .jl files parallel using the Julia Terminal.
I tried include("file1.jl" & "file2.jl") and include("file1.jl") & include("file2.jl") but this doesn't work.
I'm not sure exactly what you want to do but if you wanted to run these two files on two different workers from the julia terminal you could for e.g.
addprocs(1) # add a worker
pmap(include,["file1.jl", "file2.jl"]) # apply include to each element
# of the array in parallel
But I'm pretty sure there will be a better way of doing whatever you want to accomplish.
While you can probably wrangle your code into the Julia parallel computing paradigms, it seems like the simplest solution is to execute your Julia scripts from the command line. Here I assume that you are comfortable allowing your CPU to handle the task scheduling, which may or may not result in parallel execution.
What follows below is a skeleton pipeline to get you started. Replace task.jl with your file1.jl, file2.jl, etc.
task.jl
println("running like a cheetah")
run_script.sh
echo `date`
julia task.jl
julia task.jl
echo `date`
run_script_parallel.sh
echo `date`
julia task.jl &
julia task.jl &
wait # do not return before background tasks are complete
echo `date`
From the command line, ensure that your BASH scripts are executable:
chmod +rwx run_script.sh run_script_parallel.sh
Try running the scripts now. Note that my example Julia script task.jl returns almost immediately, so this particular comparison is a little silly:
./run_script.sh
./run_script_parallel.sh
My output
Thu Jan 5 14:24:57 PST 2017
running like a cheetah
running like a cheetah
Thu Jan 5 14:24:57 PST 2017
Thu Jan 5 14:25:05 PST 2017
running like a cheetahrunning like a cheetah
Thu Jan 5 14:25:06 PST 2017
The first output orders the print statements in a clean serial order. But observe in the second case that the text is running together. That is common behavior for parallel print statements.

How to parse travis_time:start into normal datestamp?

I'm delving into the travis-ci.org logs of a very simple app that was built to demonstrate testing and continuous integration.
I see timestamps given in each raw log file, but I don't know a simple way to convert them to readable dates?
There seem to be two different formats.
I'm on a mac, so bash or python or an online calculator answer would be terrific.
thank you!
Here are the 4 sub-builds, each for a different jvm:
4.1 https://travis-ci.org/ihassin/cucumber-jvm/jobs/40111229
travis_time:end:121a64f6:start=1415224070390184790,finish=1415224071568557470,duration=1178372680
4.2 https://travis-ci.org/ihassin/cucumber-jvm/jobs/40111230
0Ktravis_time:start:094d841c
4.3 https://travis-ci.org/ihassin/cucumber-jvm/jobs/40111231
travis_time:start:00067b60
4.4
https://travis-ci.org/ihassin/cucumber-jvm/jobs/40111232
travis_time:start:0011ba85
The timestamps are in nanoseconds. To get a normal Unix timestamp, you need to divide them by 10^6. For the value finish=1415224071568557470, for example, in JavaScript you could do:
new Date(1415224071568557470 / 1e6).toUTCString() // 'Wed, 05 Nov 2014 21:47:51 GMT'

A cron job that will never execute

Is there any way to create a cron expression for not running the job at all?
I though of using this expression :
0 0 0 1 1 ? 3099
...which will practically do the job as it will run on year 3099, but is there a cleaner way to do it?
If you're still looking for something robust even in the far future, try https://stackoverflow.com/a/13938099/1601531, where I suggest the use of February 31st in crontab entries which are never intended to execute.
0 0 5 31 2 ?
I needed a valid cron schedule (? syntax not working in my system) which resolves real dates, but to be effectively "never". My current best solution was to pick the most recent leap year and see what day Feb 29th fell on. Feb 29 2016 was a Monday, so the next Monday Feb 29 is currently furthest away.
0 0 29 2 1 yields the next 5 triggers as:
02/29/2044 00:00:00Z
02/29/2072 00:00:00Z
02/29/2112 00:00:00Z
02/29/2140 00:00:00Z
02/29/2168 00:00:00Z
Not perfect but it'll do.
Pick a date in the past.
That'll never happen again. Unless it's groundhog day. Just don't pick groundhog day.
I created a duplicate (click here to see) for your question, and agree with your initial proposal. After testing, it appears Quartz will never execute a cron expression with a year above 2300.
Opening your crontab file and deleting the entry would be the adequate way. Or you might as well simlink the executable that's called by the cronjob to an empty shell script.
Tell us more about your setup, then we'll see ...
Comment it out — put # sign in front of it. Very useful, especially if you're afraid you'll forget about your changes by the year 3099.
Another possibility is to make it execute dummy command, like true rm -rf /.

Getting the time zone as a UTC offset on windows and linux

What is the simplest way to get the machine's time zone as a positive or negative UTC offset, preferably using some time of shell command?
For all Unix-ish operating systems, when using the GNU date command:
date +%z
Example, for Eastern European Time (my timezone):
[moocha#helium ~]$ date +%z
+0200
If what you want is the non-summer/daylight-savings offset, you'd have to do something like:
date -d 'Jan 1' +%z
(or Jul in the southern hemisphere). This works with date from GNU coreutils, anyway.
Shockingly enough, I don't see any way to get the tm_isdst flag from date.
I figure that if worse comes to worse I can just send a request to an NTP server and take the difference of it and the current local time, but that seems kind of wasteful if the system knows its offset.

Resources