Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Created December 16, 2011 20:18
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzztaiki/1487781 to your computer and use it in GitHub Desktop.
Save buzztaiki/1487781 to your computer and use it in GitHub Desktop.
gjs-io-sample.js
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
let [res, out, err, status] = GLib.spawn_command_line_sync('ls -la');
print(out);
let [res, out] = GLib.spawn_command_line_sync('ls -la');
print(out);
let [res, out] = GLib.spawn_sync(null, ['/bin/ls', '-la'], null, 0, null);
print(out);
let [res, out] = GLib.spawn_sync(GLib.getenv('HOME'), ['/bin/ls', '-la'], null, 0, null);
print(out);
let [res, out] = GLib.spawn_sync(null, ['ls', '-la'], null, GLib.SpawnFlags.SEARCH_PATH, null);
print(out);
GLib.spawn_command_line_async('ls -la');
let [res, pid, in_fd, out_fd, err_fd] = GLib.spawn_async_with_pipes(null, ['/bin/cat'], null, 0, null);
let out_reader = new Gio.DataInputStream({
base_stream: new Gio.UnixInputStream({fd: out_fd})
});
let in_writer = new Gio.DataOutputStream({
base_stream: new Gio.UnixOutputStream({fd: in_fd})
});
let data = ["hoge", "fuga", ""].join("\n");
in_writer.put_string(data, null);
let [out, size] = out_reader.read_line(null);
print(out);
let [out, size] = out_reader.read_line(null);
print(out);
@ondrejkolin
Copy link

When I developed a cinnamon applet it helped me to out.toString(), because out is not string!

@pkkid
Copy link

pkkid commented Apr 20, 2014

After a few iterations of calling spawn_async_with_pipes() I get the error 'Failed to create pipe for communicating with child process (Too many open files)' I tried closing everything I can, but I must be missing something.

@wieczorek1990
Copy link

With output:

let [result, stdout, stderr] = GLib.spawn_command_line_sync(command);

@andyholmes
Copy link

For those finding this in a search, there's a good example of a asynchronous file reader class here: https://github.com/optimisme/gjs-examples/blob/master/egSpawn.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment