自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Linux中使用shell腳本自動(dòng)創(chuàng)建/移除并掛載交換文件

系統(tǒng) Linux
今天我發(fā)現(xiàn)了一個(gè) Gary Stafford 寫的 shell 小腳本(兩個(gè) shell 腳本,一個(gè)用于創(chuàng)建交換文件,另外一個(gè)用于移除交換文件),它可以幫助我們?cè)?Linux 中創(chuàng)建/移除并且自動(dòng)掛載交換文件。

在Linux中使用shell腳本自動(dòng)創(chuàng)建/移除并掛載交換文件

幾天前我們寫了一篇關(guān)于在 Linux 中 3 種創(chuàng)建交換文件的方法,它們是常見的方法,但是需要人工操作。

今天我發(fā)現(xiàn)了一個(gè) Gary Stafford[1] 寫的 shell 小腳本(兩個(gè) shell 腳本,一個(gè)用于創(chuàng)建交換文件,另外一個(gè)用于移除交換文件),它可以幫助我們?cè)?Linux 中創(chuàng)建/移除并且自動(dòng)掛載交換文件。

默認(rèn)這個(gè)腳本創(chuàng)建并掛載 512MB 的交換文件。如果你想要更多的交換空間和不同的文件名,你需要相應(yīng)地修改腳本。修改腳本不是一件困難的事,因?yàn)檫@是一個(gè)容易上手而且很小的腳本。

推薦閱讀: Linux 中 3 種簡易創(chuàng)建或擴(kuò)展交換空間的方法

如何檢查當(dāng)前交換文件大小

使用 free[3] 和 swapon 命令檢查已經(jīng)存在交換空間。

  1. free -h 
  2.  
  3.               total        used        free      shared  buff/cache   available 
  4.  
  5. Mem:           2.0G        1.3G        139M         45M        483M        426M 
  6.  
  7. Swap:          2.0G        655M        1.4G 
  8.  
  9. $ swapon --show 
  10.  
  11. NAME      TYPE      SIZE   USED PRIO 
  12.  
  13. /dev/sda5 partition   2G 655.2M   -1 

上面的輸出顯示我當(dāng)前的交換空間是 2GB。

創(chuàng)建交換文件

創(chuàng)建 create_swap.sh 文件并添加下面的內(nèi)容來自動(dòng)化交換空間的創(chuàng)建和掛載。

  1. $ nano create_swap.sh 
  2.  
  3. #!/bin/sh 
  4.  
  5. size of swapfile in megabytes 
  6.  
  7. swapsize=1024 
  8.  
  9. # does the swap file already exist? 
  10.  
  11. grep -q "swapfile" /etc/fstab 
  12.  
  13. # if not then create it 
  14.  
  15. if [ $? -ne 0 ]; then 
  16.  
  17.     echo 'swapfile not found. Adding swapfile.' 
  18.  
  19.     fallocate -l ${swapsize}M /swapfile 
  20.  
  21.     chmod 600 /swapfile 
  22.  
  23.     mkswap /swapfile 
  24.  
  25.     swapon /swapfile 
  26.  
  27.     echo '/swapfile none swap defaults 0 0' >> /etc/fstab 
  28.  
  29. else 
  30.  
  31.     echo 'swapfile found. No changes made.' 
  32.  
  33. fi 
  34.  
  35. echo '--------------------------------------------' 
  36.  
  37. echo 'Check whether the swap space created or not?' 
  38.  
  39. echo '--------------------------------------------' 
  40.  
  41. swapon --show 

給文件添加執(zhí)行權(quán)限。

  1. $ sudo +x create_swap.sh 

運(yùn)行文件來創(chuàng)建和掛載交換文件。

  1. $ sudo ./create_swap.sh 
  2.  
  3. swapfile not found. Adding swapfile. 
  4.  
  5. Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) 
  6.  
  7. no label, UUID=d9004261-396a-4321-a45f-9923e3e1328c 
  8.  
  9. -------------------------------------------- 
  10.  
  11. Check whether the swap space created or not
  12.  
  13. -------------------------------------------- 
  14.  
  15. NAME      TYPE       SIZE   USED PRIO 
  16.  
  17. /dev/sda5 partition    2G 954.1M   -1 
  18.  
  19. /swapfile file      1024M     0B   -2 

你可以看到新的 1024M 的 swapfile。重啟系統(tǒng)以使用新的交換文件。

移除交換文件

如果不再需要交換文件,接著創(chuàng)建 remove_swap.sh 文件并添加下面的內(nèi)容來移除交換文件以及它的 /etc/fstab 掛載點(diǎn)。

  1. $ nano remove_swap.sh 
  2.  
  3. #!/bin/sh 
  4.  
  5. # does the swap file exist? 
  6.  
  7. grep -q "swapfile" /etc/fstab 
  8.  
  9. # if it does then remove it 
  10.  
  11. if [ $? -eq 0 ]; then 
  12.  
  13.     echo 'swapfile found. Removing swapfile.' 
  14.  
  15.     sed -i '/swapfile/d' /etc/fstab 
  16.  
  17.     echo "3" > /proc/sys/vm/drop_caches 
  18.  
  19.     swapoff -a 
  20.  
  21.     rm -f /swapfile 
  22.  
  23. else 
  24.  
  25.     echo 'No swapfile found. No changes made.' 
  26.  
  27. fi 
  28.  
  29. echo '--------------------------------------------' 
  30.  
  31. echo 'Check whether the swap space removed or not?' 
  32.  
  33. echo '--------------------------------------------' 
  34.  
  35. swapon --show 

并給文件添加可執(zhí)行權(quán)限。

  1. $ sudo +x remove_swap.sh 

運(yùn)行腳本來移除并卸載交換文件。

  1. $ sudo ./remove_swap.sh  
  2. swapfile found. Removing swapfile. 
  3.  
  4. swapoff: /dev/sda5: swapoff failed: Cannot allocate memory 
  5.  
  6. -------------------------------------------- 
  7.  
  8. Check whether the swap space removed or not
  9.  
  10. -------------------------------------------- 
  11.  
  12. NAME      TYPE      SIZE   USED PRIO 
  13.  
  14. /dev/sda5 partition   2G 951.8M   -1  
責(zé)任編輯:龐桂玉 來源: Linux開源社區(qū)
相關(guān)推薦

2015-06-19 11:04:29

linux運(yùn)維

2019-09-16 11:40:49

Linux交換文件

2010-04-27 14:55:53

Unix SFTP

2022-07-02 15:06:06

Pandoc文件Markdown

2021-03-14 09:28:24

Linux Shell腳本

2022-10-09 10:18:44

LinuxShell腳本

2024-02-19 16:15:07

2017-04-05 13:17:26

LinuxShellvi

2012-05-08 11:11:43

Linuxcrontab命令

2019-10-22 17:33:57

LinuxBash腳本

2009-12-14 13:33:31

linuxramdisk文件系統(tǒng)

2018-04-26 09:30:54

LinuxLVM交換分區(qū)

2020-08-31 08:51:10

Linux轉(zhuǎn)儲(chǔ)文件內(nèi)存

2020-10-13 19:04:58

Bash信號(hào)捕獲Shell腳本

2020-07-29 07:40:19

Linux系統(tǒng)Vim

2014-08-08 16:17:49

shell腳本linux

2023-01-13 12:37:43

Bashshell花括號(hào)

2022-06-09 08:07:15

Shell腳本Linux

2013-05-14 10:13:06

WindowsLinux操作系統(tǒng)

2023-08-12 15:05:26

Linuxcp 命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)