{"id":147,"date":"2018-10-27T17:13:04","date_gmt":"2018-10-27T17:13:04","guid":{"rendered":"http:\/\/www.hammerdb.com\/blog\/?p=147"},"modified":"2019-08-15T17:30:06","modified_gmt":"2019-08-15T17:30:06","slug":"driving-hammerdbcli-from-a-bash-script","status":"publish","type":"post","link":"https:\/\/www.hammerdb.com\/blog\/uncategorized\/driving-hammerdbcli-from-a-bash-script\/","title":{"rendered":"Driving hammerdbcli from a bash script"},"content":{"rendered":"<p><strong>Update:<\/strong> There is considerably more efficient method of running HammerDB through a bash script described in a more recent post here: \u00a0<a href=\"https:\/\/www.hammerdb.com\/blog\/uncategorized\/running-hammerdbcli-from-a-bash-or-batch-script-updated-for-v3-2\/\">Running hammerdbcli from a bash or batch script updated for v3.2<\/a> It is strongly recommended to move to this method described for v3.2 rather than the method described in this post which can have a greater overhead and a potential impact on performance imposed by bash.<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">A current FAQ is how to drive the command line hammerdbcli from an external bash script. <\/span><span style=\"font-family: verdana, geneva, sans-serif;\">This post gives an example of one way which this can be done. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">For an example we will run a bash script that prints output and then runs hammerdbcli to build an Oracle schema before wating for this to finish and printing more output.\u00a0 Firstly we need the hammerdbcli script.\u00a0 Note that as HammerDB is multithreaded, adding exit to the end of a script will cause that script to terminate before virtual users are complete. This is unlikely to be the desired outcome. Therefore you need a function that will wait for the script to complete before issuing the exit. The important command here is vucomplete and you need to wait until the condition of this is &#8220;true&#8221;. An example of this is as follows: <\/span><\/p>\n<pre>#!\/bin\/tclsh\nputs \"SETTING CONFIGURATION\"\nglobal complete\nproc wait_to_complete {} {\nglobal complete\nset complete [vucomplete]\nif {!$complete} {after 5000 wait_to_complete} else { exit }\n}\ndbset db ora\ndiset connection system_password oracle\ndiset connection instance vulpdb1 \ndiset tpcc count_ware 4\ndiset tpcc num_vu 4\ndiset tpcc tpcc_def_tab users\nprint dict\nbuildschema\nwait_to_complete<\/pre>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">This script has been saved as buildora.tcl.\u00a0 Now we want the driving bash script that looks as follows: <\/span><\/p>\n<pre>#!\/bin\/bash\necho \"BASH SCRIPT BEFORE BUILD..\"\n.\/hammerdbcli &lt;&lt;!\nsource buildora.tcl\n!\necho \"BASH SCRIPT AFTER BUILD..\"<\/pre>\n<p>if you want to redirect your output to a file it can look like the following:<\/p>\n<pre>#!\/bin\/bash\necho \"BASH SCRIPT BEFORE BUILD..\"\n.\/hammerdbcli &lt;&lt;!&gt;&gt; buildora.output 2&gt;&amp;1\nsource buildora.tcl\n!\necho \"BASH SCRIPT AFTER BUILD..\"<\/pre>\n<p>If you run the bash script it will now call hammerdbcli that will run buildora.tcl, this will wait until complete to call exit before returning control back to the bash script. This is just one example of many ways in which hammerdbcli can be blended with Tcl, bash or even <a href=\"https:\/\/www.hammerdb.com\/blog\/uncategorized\/how-to-use-python-with-hammerdb\/\">Python<\/a> to automate HammerDB functionality.<\/p>\n<p>If it is needed to strip the control characters from the output this can be done using sed. The stdbuf command can also be used to write the output line by line to the output file.<\/p>\n<pre>#!\/bin\/bash\necho \"BASH SCRIPT BEFORE BUILD..\"\n.\/hammerdbcli &lt;&lt;! 2&gt;&amp;1 | stdbuf -oL -eL sed -e \"s,\\x1B\\[[0-9;]*[a-zA-Z],,g\" -e \"s,\\r,,g\" -e \"s,hammerdb&gt;,,g\" -e \"s,after\\#[0-9]*,,g\" &gt;&gt; buildora.output\nsource buildora.tcl\n!\necho \"BASH SCRIPT AFTER BUILD..\"<\/pre>\n<p><script><br \/>\n  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){<br \/>\n  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),<br \/>\n  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)<br \/>\n  })(window,document,'script','https:\/\/www.google-analytics.com\/analytics.js','ga');<br \/>\n  ga('create', 'UA-87720879-1', 'auto');<br \/>\n  ga('send', 'pageview');<br \/>\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: There is considerably more efficient method of running HammerDB through a bash script described in a more recent post here: \u00a0Running hammerdbcli from a bash or batch script updated for v3.2 It is strongly recommended to move to this method described for v3.2 rather than the method described in this post which can have &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.hammerdb.com\/blog\/uncategorized\/driving-hammerdbcli-from-a-bash-script\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Driving hammerdbcli from a bash script&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"ppma_author":[5],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"authors":[{"term_id":5,"user_id":2,"is_guest":0,"slug":"hammerdb","display_name":"HammerDB","avatar_url":{"url":"https:\/\/www.hammerdb.com\/blog\/wp-content\/uploads\/2018\/10\/logo-white.png","url2x":"https:\/\/www.hammerdb.com\/blog\/wp-content\/uploads\/2018\/10\/logo-white.png"},"author_category":"","user_url":"http:\/\/www.hammerdb.com","last_name":"","first_name":"","job_title":"","description":""}],"_links":{"self":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":14,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions\/534"}],"wp:attachment":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/tags?post=147"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}