[{"data":1,"prerenderedAt":1260,"buildId":1261},["ShallowReactive",2],{"docs:\u002Fguide\u002Fexec":3},{"page":4,"section":16,"document":17,"credits":10,"prev":1252,"next":1256},{"id":5,"path":6,"file":7,"title":8,"sourceTitle":8,"description":9,"tagline":9,"eyebrow":10,"credits":10,"keywords":11,"icon":12,"layout":13,"sidebar":14,"toc":14,"kind":10,"group":10,"navigation":14,"order":15},"exec","\u002Fguide\u002Fexec","guide\u002Fexec.md","Exec","Run a command to the end without throwing on its exit code, with support for timeouts and cancellation.",null,[],"i-lucide-terminal","article",true,5,"guide",{"nodes":18,"toc":1237},[19,27,162,170,188,271,281,288,447,457,463,476,482,492,616,622,638,754,760,769,775,788,799,813,827,834,842,855,890,896,911,1007,1029,1070,1083,1089,1110],[20,21,22,26],"p",{},[23,24,25],"code",{},"fleethosts\u002Fexec"," runs local commands. The hosts API is built on it, and you can use it on its own.",[28,29,32],"pre",{"language":30,"class":31,"wrap":14},"ts","shiki shiki-themes github-light-default github-dark-default dark:github-dark-default",[23,33,35,58,59,58,61,58,113,58,136],{"class":34},"language-ts",[36,37,40,44,48,51,55],"span",{"class":38,"style":39},"line","display: inline",[36,41,43],{"style":42},"color:#CF222E;--shiki-dark:#FF7B72","import",[36,45,47],{"style":46},"color:#1F2328;--shiki-dark:#E6EDF3"," { exec } ",[36,49,50],{"style":42},"from",[36,52,54],{"style":53},"color:#0A3069;--shiki-dark:#A5D6FF"," \"fleethosts\u002Fexec\"",[36,56,57],{"style":46},";","\n",[36,60],{"class":38,"style":39},[36,62,63,66,70,73,76,80,83,86,89,92,95,98,101,104,107,110],{"class":38,"style":39},[36,64,65],{"style":42},"const",[36,67,69],{"style":68},"color:#0550AE;--shiki-dark:#79C0FF"," run",[36,71,72],{"style":42}," =",[36,74,75],{"style":42}," await",[36,77,79],{"style":78},"color:#8250DF;--shiki-dark:#D2A8FF"," exec",[36,81,82],{"style":46},"(",[36,84,85],{"style":53},"\"git\"",[36,87,88],{"style":46},", [",[36,90,91],{"style":53},"\"status\"",[36,93,94],{"style":46},", ",[36,96,97],{"style":53},"\"--short\"",[36,99,100],{"style":46},"], { cwd: ",[36,102,103],{"style":53},"\"\u002Fsrv\u002Fapp\"",[36,105,106],{"style":46},", timeoutMs: ",[36,108,109],{"style":68},"10_000",[36,111,112],{"style":46}," });",[36,114,115,118,121,124,127,130,133],{"class":38,"style":39},[36,116,117],{"style":42},"if",[36,119,120],{"style":46}," (run.code ",[36,122,123],{"style":42},"===",[36,125,126],{"style":68}," 0",[36,128,129],{"style":46},") console.",[36,131,132],{"style":78},"log",[36,134,135],{"style":46},"(run.stdout);",[36,137,138,141,144,147,150,153,156,159],{"class":38,"style":39},[36,139,140],{"style":42},"else",[36,142,143],{"style":46}," console.",[36,145,146],{"style":78},"error",[36,148,149],{"style":46},"(run.timedOut ",[36,151,152],{"style":42},"?",[36,154,155],{"style":53}," \"too slow\"",[36,157,158],{"style":42}," :",[36,160,161],{"style":46}," run.stderr);",[163,164,168],"h2",{"id":5,"anchor":165},{"label":166,"icon":167},"Section titled \"exec\"","#",[23,169,5],{},[20,171,172,175,176,179,180,183,184,187],{},[23,173,174],{},"exec(command, args, options)"," runs the command to the end and returns an ",[23,177,178],{},"ExecResult",". A non-zero exit doesn't throw. A command that can't start doesn't throw either: ",[23,181,182],{},"spawnError"," holds the code, such as ",[23,185,186],{},"ENOENT"," when it isn't installed.",[189,190,191,204],"table",{},[192,193,194],"thead",{},[195,196,197,201],"tr",{},[198,199,200],"th",{},"Field",[198,202,203],{},"What it holds",[205,206,207,221,234,248,262],"tbody",{},[195,208,209,214],{},[210,211,212],"td",{},[23,213,23],{},[210,215,216,217,220],{},"The exit code, or ",[23,218,219],{},"null"," when a signal ended the command or it never started",[195,222,223,231],{},[210,224,225,94,228],{},[23,226,227],{},"stdout",[23,229,230],{},"stderr",[210,232,233],{},"The end of the output",[195,235,236,241],{},[210,237,238],{},[23,239,240],{},"timedOut",[210,242,243,244,247],{},"The command ran past ",[23,245,246],{},"timeoutMs"," and was stopped",[195,249,250,255],{},[210,251,252],{},[23,253,254],{},"truncated",[210,256,257,258,261],{},"The output went past ",[23,259,260],{},"maxOutput",", so only its end was kept",[195,263,264,268],{},[210,265,266],{},[23,267,182],{},[210,269,270],{},"Why the command couldn't start",[20,272,273,276,277,280],{},[23,274,275],{},"args"," go to the command as they are, with no shell, unless you pass ",[23,278,279],{},"shell: true",".",[282,283,287],"h3",{"id":284,"anchor":285},"options",{"label":286,"icon":167},"Section titled \"Options\"","Options",[189,289,290,303],{},[192,291,292],{},[195,293,294,297,300],{},[198,295,296],{},"Option",[198,298,299],{},"Default",[198,301,302],{},"What it does",[205,304,305,318,333,348,366,378,392,408,422,434],{},[195,306,307,312,315],{},[210,308,309],{},[23,310,311],{},"cwd",[210,313,314],{},"the current folder",[210,316,317],{},"Where the command runs",[195,319,320,325,330],{},[210,321,322],{},[23,323,324],{},"env",[210,326,327],{},[23,328,329],{},"process.env",[210,331,332],{},"The whole environment, not additions to it",[195,334,335,340,345],{},[210,336,337],{},[23,338,339],{},"input",[210,341,342],{},[23,343,344],{},"\"\"",[210,346,347],{},"Written to stdin, which then closes",[195,349,350,355,360],{},[210,351,352],{},[23,353,354],{},"holdStdin",[210,356,357],{},[23,358,359],{},"false",[210,361,362,363,365],{},"Keep stdin open after ",[23,364,339],{},", so the command sees when this process is gone",[195,367,368,372,375],{},[210,369,370],{},[23,371,246],{},[210,373,374],{},"none",[210,376,377],{},"Stop the command after this many milliseconds",[195,379,380,385,389],{},[210,381,382],{},[23,383,384],{},"shell",[210,386,387],{},[23,388,359],{},[210,390,391],{},"Run through the shell",[195,393,394,399,401],{},[210,395,396],{},[23,397,398],{},"signal",[210,400,374],{},[210,402,403,404,407],{},"An ",[23,405,406],{},"AbortSignal"," that stops the command",[195,409,410,415,419],{},[210,411,412],{},[23,413,414],{},"keepTerminal",[210,416,417],{},[23,418,359],{},[210,420,421],{},"Keep the terminal, so the command can prompt",[195,423,424,428,431],{},[210,425,426],{},[23,427,260],{},[210,429,430],{},"1 MiB",[210,432,433],{},"Characters of stdout and of stderr to keep, from the end",[195,435,436,441,444],{},[210,437,438],{},[23,439,440],{},"stderrHead",[210,442,443],{},"0",[210,445,446],{},"Characters of stderr to keep from the start as well",[20,448,449,450,452,453,456],{},"With ",[23,451,440],{},", both ends of stderr survive, joined by a ",[23,454,455],{},"[...]"," line where the middle went.",[163,458,462],{"id":459,"anchor":460},"timeouts",{"label":461,"icon":167},"Section titled \"Timeouts\"","Timeouts",[20,464,465,466,468,469,471,472,475],{},"When ",[23,467,246],{}," passes, ",[23,470,5],{}," kills the command and resolves with ",[23,473,474],{},"timedOut: true",". It resolves once the command exits, even when something the command started still holds its output open.",[163,477,481],{"id":478,"anchor":479},"cancelling",{"label":480,"icon":167},"Section titled \"Cancelling\"","Cancelling",[20,483,484,485,487,488,491],{},"Pass a ",[23,486,398],{}," to stop a command from outside. When the signal aborts, the command and everything it started are killed, and the promise rejects with ",[23,489,490],{},"signal.reason",". A signal that has already aborted rejects before anything starts.",[28,493,494],{"language":30,"class":31,"wrap":14},[23,495,496,58,514,58,553,58,555,58,597,58,611],{"class":34},[36,497,498,500,503,505,508,511],{"class":38,"style":39},[36,499,65],{"style":42},[36,501,502],{"style":68}," controller",[36,504,72],{"style":42},[36,506,507],{"style":42}," new",[36,509,510],{"style":78}," AbortController",[36,512,513],{"style":46},"();",[36,515,516,519,522,525,528,531,533,536,539,541,544,547,550],{"class":38,"style":39},[36,517,518],{"style":78},"setTimeout",[36,520,521],{"style":46},"(() ",[36,523,524],{"style":42},"=>",[36,526,527],{"style":46}," controller.",[36,529,530],{"style":78},"abort",[36,532,82],{"style":46},[36,534,535],{"style":42},"new",[36,537,538],{"style":78}," Error",[36,540,82],{"style":46},[36,542,543],{"style":53},"\"cancelled\"",[36,545,546],{"style":46},")), ",[36,548,549],{"style":68},"5_000",[36,551,552],{"style":46},");",[36,554],{"class":38,"style":39},[36,556,557,560,562,564,567,569,572,574,577,580,583,586,589,592,594],{"class":38,"style":39},[36,558,559],{"style":42},"await",[36,561,79],{"style":78},[36,563,82],{"style":46},[36,565,566],{"style":53},"\"sh\"",[36,568,88],{"style":46},[36,570,571],{"style":53},"\"-c\"",[36,573,94],{"style":46},[36,575,576],{"style":53},"\"make all\"",[36,578,579],{"style":46},"], { signal: controller.signal }).",[36,581,582],{"style":78},"catch",[36,584,585],{"style":46},"((",[36,587,146],{"style":588},"color:#953800;--shiki-dark:#FFA657",[36,590,591],{"style":46},") ",[36,593,524],{"style":42},[36,595,596],{"style":46}," {",[36,598,599,602,604,607],{"class":38,"style":39},[36,600,601],{"style":46},"  console.",[36,603,146],{"style":78},[36,605,606],{"style":46},"(error.message); ",[36,608,610],{"style":609},"color:#6E7781;--shiki-dark:#8B949E","\u002F\u002F \"cancelled\"",[36,612,613],{"class":38,"style":39},[36,614,615],{"style":46},"});",[282,617,621],{"id":618,"anchor":619},"one-signal-for-a-whole-job",{"label":620,"icon":167},"Section titled \"One signal for a whole job\"","One signal for a whole job",[20,623,624,627,628,630,631,634,635,637],{},[23,625,626],{},"withExecSignal(signal, work)"," gives the signal to every ",[23,629,5],{}," that runs inside ",[23,632,633],{},"work",", however deep. A command that also has its own ",[23,636,398],{}," stops when either aborts.",[28,639,640],{"language":30,"class":31,"wrap":14},[23,641,642,58,655,58,657,58,671,58,696,58,741,58,750],{"class":34},[36,643,644,646,649,651,653],{"class":38,"style":39},[36,645,43],{"style":42},[36,647,648],{"style":46}," { exec, withExecSignal } ",[36,650,50],{"style":42},[36,652,54],{"style":53},[36,654,57],{"style":46},[36,656],{"class":38,"style":39},[36,658,659,661,663,665,667,669],{"class":38,"style":39},[36,660,65],{"style":42},[36,662,502],{"style":68},[36,664,72],{"style":42},[36,666,507],{"style":42},[36,668,510],{"style":78},[36,670,513],{"style":46},[36,672,673,675,678,681,684,687,690,692,694],{"class":38,"style":39},[36,674,559],{"style":42},[36,676,677],{"style":78}," withExecSignal",[36,679,680],{"style":46},"(controller.signal, ",[36,682,683],{"style":42},"async",[36,685,686],{"style":46}," (",[36,688,689],{"style":588},"settled",[36,691,591],{"style":46},[36,693,524],{"style":42},[36,695,596],{"style":46},[36,697,698,701,704,706,709,712,714,716,719,721,724,727,729,731,733,735,738],{"class":38,"style":39},[36,699,700],{"style":42},"  await",[36,702,703],{"style":68}," Promise",[36,705,280],{"style":46},[36,707,708],{"style":78},"allSettled",[36,710,711],{"style":46},"([",[36,713,5],{"style":78},[36,715,82],{"style":46},[36,717,718],{"style":53},"\"sleep\"",[36,720,88],{"style":46},[36,722,723],{"style":53},"\"5\"",[36,725,726],{"style":46},"]), ",[36,728,5],{"style":78},[36,730,82],{"style":46},[36,732,718],{"style":53},[36,734,88],{"style":46},[36,736,737],{"style":53},"\"10\"",[36,739,740],{"style":46},"])]);",[36,742,743,745,748],{"class":38,"style":39},[36,744,700],{"style":42},[36,746,747],{"style":78}," settled",[36,749,513],{"style":46},[36,751,752],{"class":38,"style":39},[36,753,615],{"style":46},[20,755,756,759],{},[23,757,758],{},"settled()"," waits for every command the job started to finish, including ones it didn't await. Call it before you clean up files they use.",[20,761,762,765,766,280],{},[23,763,764],{},"execSignal()"," returns the signal of the job you are in, or ",[23,767,768],{},"undefined",[163,770,774],{"id":771,"anchor":772},"what-gets-stopped",{"label":773,"icon":167},"Section titled \"What gets stopped\"","What gets stopped",[20,776,777,778,780,781,784,785,787],{},"On macOS and Linux, a command runs in a process group of its own when it has a signal (its own, or a job's), when ",[23,779,384],{}," is ",[23,782,783],{},"true",", or when ",[23,786,414],{}," can't be honoured. Stopping kills the whole group, so a shell's children stop with it.",[20,789,790,791,794,795,798],{},"A plain command with no signal stays in this process's group, so ",[23,792,793],{},"ssh"," and ",[23,796,797],{},"git"," can still prompt on the terminal. A timeout kills only that command.",[20,800,801,802,94,805,808,809,812],{},"When this process gets ",[23,803,804],{},"SIGINT",[23,806,807],{},"SIGTERM"," or ",[23,810,811],{},"SIGHUP"," while such commands run, it passes the signal on to them, then ends with that signal. Ctrl-C stops your commands, not only Node.",[20,814,449,815,818,819,822,823,826],{},[23,816,817],{},"keepTerminal: true",", the command stays in this group and keeps the terminal. To stop it, fleethosts finds what it started in the process table, freezes each process with ",[23,820,821],{},"SIGSTOP",", then kills them. On Linux this needs ",[23,824,825],{},"\u002Fproc"," to show this process's PID namespace; where it doesn't, the command gets its own group and loses the terminal.",[20,828,829,830,833],{},"On Windows, ",[23,831,832],{},"taskkill \u002Ft"," stops the command and what it started.",[282,835,839],{"id":836,"anchor":837},"stopall",{"label":838,"icon":167},"Section titled \"stopAll\"",[23,840,841],{},"stopAll",[20,843,844,847,848,850,851,854],{},[23,845,846],{},"stopAll()"," stops the commands ",[23,849,5],{}," is still tracking, using the process and group rules above. With default ",[23,852,853],{},"exec()"," options and no job signal on macOS or Linux, only the direct child is killed; its descendants may survive. You can call it on exit, but it does not guarantee process-tree cleanup:",[28,856,857],{"language":30,"class":31,"wrap":14},[23,858,859,58,872,58,874],{"class":34},[36,860,861,863,866,868,870],{"class":38,"style":39},[36,862,43],{"style":42},[36,864,865],{"style":46}," { stopAll } ",[36,867,50],{"style":42},[36,869,54],{"style":53},[36,871,57],{"style":46},[36,873],{"class":38,"style":39},[36,875,876,879,882,884,887],{"class":38,"style":39},[36,877,878],{"style":46},"process.",[36,880,881],{"style":78},"on",[36,883,82],{"style":46},[36,885,886],{"style":53},"\"exit\"",[36,888,889],{"style":46},", stopAll);",[163,891,894],{"id":797,"anchor":892},{"label":893,"icon":167},"Section titled \"git\"",[23,895,797],{},[20,897,898,901,902,905,906,780,909,280],{},[23,899,900],{},"git(args, options)"," runs git with no prompts, and throws a ",[23,903,904],{},"GitError"," on a non-zero exit unless ",[23,907,908],{},"allowFail",[23,910,783],{},[28,912,913],{"language":30,"class":31,"wrap":14},[23,914,915,58,928,58,930,58,966],{"class":34},[36,916,917,919,922,924,926],{"class":38,"style":39},[36,918,43],{"style":42},[36,920,921],{"style":46}," { git, gitOut } ",[36,923,50],{"style":42},[36,925,54],{"style":53},[36,927,57],{"style":46},[36,929],{"class":38,"style":39},[36,931,932,934,937,939,941,944,946,949,951,954,956,959,962,964],{"class":38,"style":39},[36,933,65],{"style":42},[36,935,936],{"style":68}," branch",[36,938,72],{"style":42},[36,940,75],{"style":42},[36,942,943],{"style":78}," gitOut",[36,945,711],{"style":46},[36,947,948],{"style":53},"\"rev-parse\"",[36,950,94],{"style":46},[36,952,953],{"style":53},"\"--abbrev-ref\"",[36,955,94],{"style":46},[36,957,958],{"style":53},"\"HEAD\"",[36,960,961],{"style":46},"], ",[36,963,103],{"style":53},[36,965,552],{"style":46},[36,967,968,970,973,975,978,981,983,986,988,991,993,996,998,1000,1003,1005],{"class":38,"style":39},[36,969,65],{"style":42},[36,971,972],{"style":46}," { ",[36,974,23],{"style":68},[36,976,977],{"style":46}," } ",[36,979,980],{"style":42},"=",[36,982,75],{"style":42},[36,984,985],{"style":78}," git",[36,987,711],{"style":46},[36,989,990],{"style":53},"\"diff\"",[36,992,94],{"style":46},[36,994,995],{"style":53},"\"--quiet\"",[36,997,100],{"style":46},[36,999,103],{"style":53},[36,1001,1002],{"style":46},", allowFail: ",[36,1004,783],{"style":68},[36,1006,112],{"style":46},[20,1008,1009,1010,1013,1014,794,1017,1020,1021,1024,1025,1028],{},"It sets ",[23,1011,1012],{},"GIT_TERMINAL_PROMPT=0",", empties ",[23,1015,1016],{},"GIT_ASKPASS",[23,1018,1019],{},"SSH_ASKPASS",", runs with ",[23,1022,1023],{},"LC_ALL=C",", and drops ",[23,1026,1027],{},"GIT_CONFIG",". An http transfer under 1 KB\u002Fs for a minute ends. The timeout defaults to two minutes, and the output is never cut.",[189,1030,1031,1039],{},[192,1032,1033],{},[195,1034,1035,1037],{},[198,1036,296],{},[198,1038,302],{},[205,1040,1041,1051,1061],{},[195,1042,1043,1048],{},[210,1044,1045],{},[23,1046,1047],{},"noFetch",[210,1049,1050],{},"Fetch nothing from a remote, not even a lazy fetch in a partial clone",[195,1052,1053,1058],{},[210,1054,1055],{},[23,1056,1057],{},"indexFile",[210,1059,1060],{},"Use another index file",[195,1062,1063,1067],{},[210,1064,1065],{},[23,1066,339],{},[210,1068,1069],{},"Written to stdin",[20,1071,1072,1073,1075,1076,1078,1079,1082],{},"A ",[23,1074,904],{}," has the ",[23,1077,275],{}," and the ",[23,1080,1081],{},"result",". Its message says git isn't installed, that it timed out, or what git printed.",[163,1084,1088],{"id":1085,"anchor":1086},"finding-a-program",{"label":1087,"icon":167},"Section titled \"Finding a program\"","Finding a program",[20,1090,1091,1094,1095,1098,1099,808,1102,1105,1106,1109],{},[23,1092,1093],{},"program(variable, fallback)"," returns the command to run: ",[23,1096,1097],{},"fallback",", or the program the environment variable names. A ",[23,1100,1101],{},".js",[23,1103,1104],{},".mjs"," file runs with the current Node. ",[23,1107,1108],{},"notInstalled(command, fallback, variable)"," writes the error for a missing one.",[28,1111,1112],{"language":30,"class":31,"wrap":14},[23,1113,1114,58,1127,58,1129,58,1162,58,1199],{"class":34},[36,1115,1116,1118,1121,1123,1125],{"class":38,"style":39},[36,1117,43],{"style":42},[36,1119,1120],{"style":46}," { exec, notInstalled, program } ",[36,1122,50],{"style":42},[36,1124,54],{"style":53},[36,1126,57],{"style":46},[36,1128],{"class":38,"style":39},[36,1130,1131,1133,1135,1138,1140,1143,1145,1147,1150,1152,1155,1157,1160],{"class":38,"style":39},[36,1132,65],{"style":42},[36,1134,972],{"style":46},[36,1136,1137],{"style":68},"command",[36,1139,94],{"style":46},[36,1141,1142],{"style":68},"prefix",[36,1144,977],{"style":46},[36,1146,980],{"style":42},[36,1148,1149],{"style":78}," program",[36,1151,82],{"style":46},[36,1153,1154],{"style":53},"\"MYTOOL_RSYNC\"",[36,1156,94],{"style":46},[36,1158,1159],{"style":53},"\"rsync\"",[36,1161,552],{"style":46},[36,1163,1164,1166,1168,1170,1172,1174,1177,1180,1183,1186,1188,1191,1193,1196],{"class":38,"style":39},[36,1165,65],{"style":42},[36,1167,69],{"style":68},[36,1169,72],{"style":42},[36,1171,75],{"style":42},[36,1173,79],{"style":78},[36,1175,1176],{"style":46},"(command, [",[36,1178,1179],{"style":42},"...",[36,1181,1182],{"style":46},"prefix, ",[36,1184,1185],{"style":53},"\"-a\"",[36,1187,94],{"style":46},[36,1189,1190],{"style":53},"\"src\u002F\"",[36,1192,94],{"style":46},[36,1194,1195],{"style":53},"\"backup\u002F\"",[36,1197,1198],{"style":46},"]);",[36,1200,1201,1203,1206,1208,1211,1213,1216,1218,1220,1222,1225,1228,1230,1232,1234],{"class":38,"style":39},[36,1202,117],{"style":42},[36,1204,1205],{"style":46}," (run.spawnError ",[36,1207,123],{"style":42},[36,1209,1210],{"style":53}," \"ENOENT\"",[36,1212,591],{"style":46},[36,1214,1215],{"style":42},"throw",[36,1217,507],{"style":42},[36,1219,538],{"style":78},[36,1221,82],{"style":46},[36,1223,1224],{"style":78},"notInstalled",[36,1226,1227],{"style":46},"(command, ",[36,1229,1159],{"style":53},[36,1231,94],{"style":46},[36,1233,1154],{"style":53},[36,1235,1236],{"style":46},"));",[1238,1243,1244,1247,1250,1251],{"id":5,"depth":1239,"text":5,"children":1240},2,[1241],{"id":284,"depth":1242,"text":287},3,{"id":459,"depth":1239,"text":462},{"id":478,"depth":1239,"text":481,"children":1245},[1246],{"id":618,"depth":1242,"text":621},{"id":771,"depth":1239,"text":774,"children":1248},[1249],{"id":836,"depth":1242,"text":841},{"id":797,"depth":1239,"text":797},{"id":1085,"depth":1239,"text":1088},{"path":1253,"title":1254,"description":1255},"\u002Fguide\u002Fhosts","Hosts","Check fleet entries, expand Tailscale tags into machines, run one command on each over ssh, and record the fleet.",{"path":1257,"title":1258,"description":1259},"\u002Fguide\u002Fagents","Coding agents","Tell coding agents how your program reaches machines, and let them test it without a real host.",1796388536785,"7b1f70a7-2953-4287-ada9-b3dc097b349d"]