{"id":5321,"date":"2016-12-03T22:24:53","date_gmt":"2016-12-03T22:24:53","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5321"},"modified":"2016-12-03T22:24:53","modified_gmt":"2016-12-03T22:24:53","slug":"node-execsync-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/node-execsync-example\/","title":{"rendered":"Node: execSync example"},"content":{"rendered":"<p>In Node.js, you can run shell commands if you import child process:<\/p>\n<pre lang=\"javascript\">\nconst child_process = require('child_process');\n<\/pre>\n<p>You can run these inline, and using the same console output as your script, if you send &#8220;stdio&#8221; as an argument to execSync.<\/p>\n<pre lang=\"javascript\">\nconst cmd = 'd:\/Software\/ffmpeg-20160619-5f5a97d-win32-static\/bin\/ffmpeg.exe -i ' +  id + '.en.vtt ' + id + '.srt';\n\nchild_process.execSync(\n  cmd, \n  {\n    cwd: 'd:\/projects\/youtube-scraper\/videos',\n    stdio: [0, 1, 2]\n  });\n<\/pre>\n<p>If you don&#8217;t change stdio, you will get a buffer back, which contains the output of the script.<\/p>\n<p>Changing the working directory is also handy. I found experimentally that if you do this on Windows, you need to use Windows style paths (note: if you are using the new bash for Windows, you instead need to treat this as if it were Ubuntu).<\/p>\n<p>I also found experimentally that some shell scripts will pop up Windows, and run asynchronously. I was able to fix this by inlining the shell script into separate Node commands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to run command line utiliiies from inside Node.js on Windows<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,387,388],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5321"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5321"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5321\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}