gpt4 book ai didi

ruby - 打印作业编程未取消

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:26 25 4
gpt4 key购买 nike

我创建了一个程序,它将扫描用户指定的服务器并输出该服务器上的打印作业列表,从那里它将输出所有早于 2016 年的作业,以及 gsub直到工作 ID 号。

然后它应该做的是取消作业,但它正在做的是说:

Cancelling job: 829116
cancel: cancel-job failed: Job #829116 does not exist!
Cancelling job: 795259
cancel: cancel-job failed: Job #795259 does not exist!
Cancelling job: 795260
cancel: cancel-job failed: Job #795260 does not exist!
Cancelling job: 797216
cancel: cancel-job failed: Job #797216 does not exist!
Cancelling job: 797217
cancel: cancel-job failed: Job #797217 does not exist!

当它确实存在时,如果我转到特定服务器并运行 prt_jobs,作业就会弹出。示例:

#On the server I ran prt_jobs
cdprt11-829116 dij 244736 Mon Aug 24 06:07:12 2015
closing_2-795259 dlc 228352 Tue Jul 28 13:43:08 2015
closing_2-795260 dlc 228352 Tue Jul 28 13:44:05 2015
closing_2-797216 tkb 286720 Wed Jul 29 13:56:27 2015
closing_2-797217 tkb 250880 Wed Jul 29 13:56:41 2015

谁能告诉我为什么它不取消指定的作业?

来源:

#!/usr/local/bin/ruby

require 'rubygems'
require 'net/ssh'
require 'etc'

class PrintJobs

HOST = ARGV[0]
USERNAME = Etc.getlogin
PASSWORD = nil

def scan_for_jobs
check_jobs = Net::SSH.start(HOST, USERNAME, :password => PASSWORD) do |ssh|
cmd = "prt_jobs"
info = ssh.exec!(cmd)
if info == nil
puts "No print jobs on server #{HOST}"
else
res = info.split("\n").reject {|line| line.match(/\s+2016/s+/)}.join("\n")
puts res
print "Process into kill que: "
input = STDIN.gets.chomp.upcase
if input == "YES"
kill_que(check_jobs, res)
else
exit 1
end
end
end
end

def kill_que(check_jobs, res)
puts "Loading jobs into kill que.."
column = 0
job_ids = res.lines.map { |job| job.split(/\s+/)[0] }.each do |task_id|
kill_jobs(task_id)
end
end

def kill_jobs(task_id)
id_to_strip = task_id.gsub(/\-/," ")
column = 1
stripping_id = id_to_strip.lines.map { |task| task.split(/\s+/)[1] }.each do |id|
id.strip
puts "Cancelling job: #{id}"
`sudo cancel #{id}`
# puts id
end
end
end

test = PrintJobs.new
test.scan_for_jobs

最佳答案

正如问题评论中所指出的,cancel 命令是在本地服务器上发出的,而不是在远程服务器上发出的。您需要将 ssh 对象传递给 kill_jobs 并在 ssh 连接上执行命令。

第 1 步:将 ssh 传递给 kill_que 方法。

   if input == "YES"
kill_que(ssh, check_jobs, res) # *** Pass ssh object

第 2 步:更新kill_que 方法以使用ssh 对象

def kill_que(ssh, check_jobs, res)
puts "Loading jobs into kill que.."
column = 0
job_ids = res.lines.map { |job| job.split(/\s+/)[0] }.each do |task_id|
kill_jobs(ssh, task_id) # *** Pass ssh to kill_jobs
end
end

第 3 步:在 kill_jobs 中使用 ssh

def kill_jobs(ssh, task_id)
id_to_strip = task_id.gsub(/\-/," ")
column = 1
stripping_id = id_to_strip.lines.map { |task| task.split(/\s+/)[1] }.each do |id|
id.strip
puts "Cancelling job: #{id}"
ssh.exec!("sudo cancel #{id}") # *** Issue this command via ssh
# puts id
end
end

关于ruby - 打印作业编程未取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34622878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com