Skip to content
标签
欢迎扫码关注公众号

[Jenkins]在 Pipeline 中远程停止/启动计划任务

停止/启动计划任务使用 schtasks 命令(参考这篇博客)。

启动定时任务

bat
schtasks /run /tn "Task-Name" /s 192.168.0.1 /u domain\jiajia /p jiajia'spassword

停止定时任务

bat
schtasks /end /tn "Task-Name" /s 192.168.0.1 /u domain\jiajia /p jiajia'spassword

pipeline 中使用 bat 方法执行上述的 Windows 批处理脚本。

groovy
stage('End Task') {
    steps {
        withCredentials([usernamePassword(credentialsId: 'oct_liujiajia', passwordVariable: 'OCT_USER_PASSWORD', usernameVariable: 'OCT_USER_NAME')]) {
            bat label: '停止计划任务', script: 'schtasks /end /tn "Task-Name" /s 192.168.0.1 /u %OCT_USER_NAME% /p %OCT_USER_PASSWORD%'
        }
    }
}
stage('Run Task') {
    steps {
        withCredentials([usernamePassword(credentialsId: 'oct_liujiajia', passwordVariable: 'OCT_USER_PASSWORD', usernameVariable: 'OCT_USER_NAME')]) {
            bat label: '启动计划任务', script: 'schtasks /run /tn "Task-Name" /s 192.168.0.1 /u %OCT_USER_NAME% /p %OCT_USER_PASSWORD%'
        }
    }
}

这里使用了 withCredentials 方法来获取 Jenkins 中配置的全局凭据,以避免在脚本中使用明文的账号和密码。

全局凭据在 Jenkins → 凭据 → 系统 → 全局凭据 (unrestricted) 中设置,这里使用的是 Username with password 类型的凭据。如果是域用户的话,用户名中不要忘记加上域名。

withCredentials 方法后面的 usernamePassword 即表示获取的是 Username with password 类型的凭据,此时有三个参数:

  • credentialsId :在凭据设置页面指定的凭据ID
  • passwordVariable :密码对应的变量名
  • usernameVariable :用户名对应的变量名

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.