{"id":6788,"date":"2024-11-12T14:43:18","date_gmt":"2024-11-12T14:43:18","guid":{"rendered":"https:\/\/www.ktchost.com\/blog\/?p=6788"},"modified":"2024-11-12T14:47:44","modified_gmt":"2024-11-12T14:47:44","slug":"jenkinsfile-to-execute-command-on-linux-servers","status":"publish","type":"post","link":"https:\/\/www.ktchost.com\/blog\/jenkinsfile-to-execute-command-on-linux-servers\/","title":{"rendered":"Jenkinsfile to execute command on Linux servers"},"content":{"rendered":"\n<p>To execute commands on a Linux machine from a <strong>Jenkinsfile<\/strong>, you can use the <code>sh<\/code> step, which allows you to run shell commands. This can be done in either a <strong>Declarative Pipeline<\/strong> or <strong>Scripted Pipeline<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Example: Simple Jenkinsfile to Run Linux Commands (<strong>Declarative Jenkinsfile<\/strong>)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pipeline {\n    agent any  \/\/ This can be adjusted to a specific agent or label if needed\n\n    stages {\n        stage('Run Linux Commands') {\n            steps {\n                script {\n                    \/\/ Running a simple shell command on Linux\n                    sh 'echo \"Hello, Jenkins!\"'\n                    sh 'ls -l'  \/\/ List the directory contents\n                    sh 'uptime'  \/\/ Display the system uptime\n                }\n            }\n        }\n    }\n    post {\n        success {\n            echo 'Linux commands executed successfully.'\n        }\n        failure {\n            echo 'Error executing Linux commands.'\n        }\n    }\n}\n\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Key Points:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>sh<\/code> Step<\/strong>: This is used to execute Linux shell commands. Anything you can run in the terminal, you can run through Jenkins.\n<ul class=\"wp-block-list\">\n<li>For example, <code>sh 'echo \"Hello, Jenkins!\"'<\/code> will print <code>Hello, Jenkins!<\/code> in the console output.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Multiple Commands<\/strong>: You can execute multiple commands in a single <code>sh<\/code> step, or use separate <code>sh<\/code> steps for different commands.<\/li>\n\n\n\n<li><strong>Error Handling<\/strong>: The <code>post<\/code> block helps handle success or failure scenarios after the pipeline completes.<\/li>\n<\/ol>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example: Simple Jenkinsfile to Run Linux Commands (Scripted Jenkinsfile)<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-left\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node {\n    stage('Run Linux Commands') {\n        try {\n            \/\/ Running multiple shell commands\n            sh 'echo \"Starting Linux Command Execution\"'\n            sh 'df -h'  \/\/ Display disk space usage\n            sh 'top -n 1'  \/\/ Display system processes once\n        } catch (Exception e) {\n            currentBuild.result = 'FAILURE'\n            throw e\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Notes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>sh<\/code> step can run any shell commands (like <code>ls<\/code>, <code>df<\/code>, <code>top<\/code>, etc.) as if you were typing them directly into the terminal.<\/li>\n\n\n\n<li>Ensure that the Jenkins agent has the necessary permissions and environment to execute the commands.<\/li>\n<\/ul>\n\n\n\n<p>If you need to run specific commands that require elevated privileges, you might need to configure Jenkins or the agent with proper sudo permissions, which can be done by using <code>sudo<\/code> in the command, but it&#8217;s important to configure it safely to avoid security risks.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>To execute commands on a Linux machine from a Jenkinsfile, you can use the sh step, which allows you to run shell commands. This can <a class=\"mh-excerpt-more\" href=\"https:\/\/www.ktchost.com\/blog\/jenkinsfile-to-execute-command-on-linux-servers\/\" title=\"Jenkinsfile to execute command on Linux servers\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6790,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[584,40,540],"tags":[587,588,593,541,542,590,583,585,592],"class_list":["post-6788","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jenkinsfile","category-scripts","category-tools","tag-execute-linux-commands","tag-jenkins-automation","tag-jenkins-build-automation","tag-jenkins-ci-cd","tag-jenkins-pipeline","tag-jenkins-pipeline-tutorial","tag-jenkinsfile","tag-linux-commands-in-jenkins","tag-linux-scripting-for-jenkins"],"_links":{"self":[{"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/posts\/6788"}],"collection":[{"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/comments?post=6788"}],"version-history":[{"count":1,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/posts\/6788\/revisions"}],"predecessor-version":[{"id":6789,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/posts\/6788\/revisions\/6789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/media\/6790"}],"wp:attachment":[{"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/media?parent=6788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/categories?post=6788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ktchost.com\/blog\/wp-json\/wp\/v2\/tags?post=6788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}