Vyacheslav
Vyacheslav

Reputation: 27211

Does os.execute block thread in lua?

In my nginx+lua app OS executing a command line something like os.execute("ls 2>&1 | tee a.txt") I want to know does it block main app? I want use command "execute-and-forgot" case. If it blocks how to fix it and execute a simple line in background thread?

Upvotes: 4

Views: 6782

Answers (2)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

os.execute() will block for the time of execution of the command you are running and since you generate some output, using io.popen won't help you much as you'd need to read from the pipe (otherwise the process will still block at some point).

A better way may be to run the process in the background: os.execute("ls >a.txt 2>&1 &"). The order of redirects > and 2> matters and the & at the end will run the command in the background, unblocking os.execute.

Upvotes: 8

Gilles Gregoire
Gilles Gregoire

Reputation: 1756

os.execute() is equivalent to system() in C, therefore it blocks the thread.

If you don't want to block, use io.popen instead.

Upvotes: 2

Related Questions