我试图编写一个脚本,以通过ID杀死一个进程,如果该进程早于5分钟。我已经知道import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var isOrientationStatusLandscape : Bool = false
var loadedIconList : [[String:Any]]?
var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
// this is a method to update layout properties after rotate action
func changeLayoutAfterRotation(tempLayout : UICollectionViewFlowLayout) {
self.removeSubview(tagvalue: 100)
tempLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
var myFrame = CGRect(x:10, y:150, width: GlobalVariables.screenWidthSize, height: GlobalVariables.screenHeightSize)
if isOrientationStatusLandscape == false
{
let screenWidthInt = Int(GlobalVariables.screenWidthSize)
let boxSize = (screenWidthInt - 50 ) / 4
tempLayout.itemSize = CGSize(width: boxSize, height: boxSize)
myFrame = CGRect(x:0, y:90, width: 414, height: ((boxSize*4) + 50))
}
else
{
let screenWidthInt = Int(GlobalVariables.screenHeightSize)
let boxSize = (screenWidthInt - 90 ) / 8
tempLayout.itemSize = CGSize(width: boxSize, height: boxSize)
myFrame = CGRect(x:0, y:140, width: 736, height: 200)
}
let collectionView:UICollectionView = UICollectionView(frame: myFrame, collectionViewLayout: layout)
collectionView.tag = 100
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
collectionView.backgroundColor = UIColor.white
self.view.addSubview(collectionView)
}
func createUICollectionView()
{
//print(self.view.frame)
//var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
print("DİKEY :\(GlobalVariables.screenHeightSize)")
let screenWidthInt = Int(GlobalVariables.screenWidthSize)
let boxSize = (screenWidthInt - 50 ) / 4
layout.itemSize = CGSize(width: boxSize, height: boxSize)
let myFrame = CGRect(x:0, y:90, width: 414, height: ((boxSize*4) + 50))
//let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
let myCollectionView:UICollectionView = UICollectionView(frame: myFrame, collectionViewLayout: layout)
myCollectionView.tag = 100
myCollectionView.dataSource = self
myCollectionView.delegate = self
myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
myCollectionView.backgroundColor = UIColor.red
self.view.addSubview(myCollectionView)
//self.view.willRemoveSubview(myCollectionView)
}
func removeSubview(tagvalue: Int){
//print("Start remove subview")
if let viewWithTag = self.view.viewWithTag(tagvalue) {
viewWithTag.removeFromSuperview()
}else{
print("subview removing is failed!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadIconsTemp()
self.createUICollectionView()
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
if self.isOrientationStatusLandscape == true {self.isOrientationStatusLandscape = false} else {self.isOrientationStatusLandscape = true}
self.changeLayoutAfterRotation(tempLayout: layout)
}
func setupView()
{
view.removeConstraints()
view.deactivateAllConstraints()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3 //loadedIconList!.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath)
myCell.backgroundColor = UIColor.blue
let anItem = self.loadedIconList![indexPath.row]
if let iconUrl = anItem["iconUrl"] as? String
{
// we can use iconUrl property
}
return myCell
}
func loadIconsTemp()
{
// loadedIconList array is loading in here. I am not sharing this long scope
}
}
中的进程ID。
$pid
这会导致错误:
./ script.sh:第9行:let:11:41:01:表达式中的语法错误(错误标记为“:41:01”)
我使用了this answer中的一些代码。任何想法如何解决这个问题?
答案 0 :(得分:1)
pid=1234
# 300 seconds = 5min
maximum_runtime=300
process_start_time=`ps -o lstart= -p $pid`
current_time=`date` #got the tiem in same format
let diff="$(($(date -d "$current_time" '+%s') - $(date -d "$process_start_time" '+%s')))"
#result for diff in secconds, calculated using date tool
if [ $diff -gt $maximum_runtime ]
then
kill -3 $pid
fi