Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.UnixOutputStream({fd: in_fd});
let data = ["hoge", "fuga", ""].join("\n");
in_writer.write(data, data.length, null);
let [out, size] = out_reader.read_line(null);
print(out);
let [out, size] = out_reader.read_line(null);
print(out);
@ondrejkolin

This comment has been minimized.

Copy link

@ondrejkolin ondrejkolin commented Jan 2, 2014

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

@pkkid

This comment has been minimized.

Copy link

@pkkid 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

This comment has been minimized.

Copy link

@wieczorek1990 wieczorek1990 commented Oct 13, 2014

With output:

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

This comment has been minimized.

Copy link

@andyholmes andyholmes commented Aug 15, 2017

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
You can’t perform that action at this time.