Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does os.execute block thread in lua?

Tags:

nginx

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?

like image 486
Vyacheslav Avatar asked Oct 17 '25 04:10

Vyacheslav


2 Answers

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.

like image 68
Paul Kulchenko Avatar answered Oct 18 '25 20:10

Paul Kulchenko


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

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

like image 31
Gilles Gregoire Avatar answered Oct 18 '25 19:10

Gilles Gregoire