linux清理内存缓存无权限,在Linux中允许非root用户删除缓存

我正在一个系统上进行性能测试,我需要确保从磁盘读取数据,并且它不仅仅是缓存(比如早期的测试).我读了

here,我可以使用命令删除缓存

echo 3 | sudo tee /proc/sys/vm/drop_caches

但请注意,即使我的帐户是管理员帐户(登录彼得),它仍然需要我的密码.我希望能够在批处理脚本中运行它而无需输入密码(因为这显然是手动的)

更多research让我进入了sudoers文件.我的计划是将上面的命令放入一个名为dropCache的单行脚本中,并编辑sudoers以便我可以在不输入密码的情况下运行它.所以我添加了这条线

ALL ALL=(ALL)NOPASSWD:/home/peter/dropCache

在我的sudoers文件的末尾(使用visudo).使用我的管理员帐户,如果我运行

sudo -l

我明白了

(ALL) NOPASSWD: /home/peter/dropCache

但是,如果我运行我的dropCache脚本,我仍然会被要求输入密码

./dropCache

[sudo] password for peter:

任何有关这方面的帮助将非常感激.我正在运行Ubuntu 12.04

谢谢

彼得

我在需要时做的是我编写了一个小程序,将编译文件的所有者更改为root,然后设置setuid位.

这是源代码:

#include

#include

#include

#include

extern void sync(void);

int main(void) {

if (geteuid() != 0) {

fprintf(stderr, "flush-cache: Not root\n");

exit(EXIT_FAILURE);

}

printf("Flushing page cache, dentries and inodes...\n");

// First: the traditional three sync calls. Perhaps not needed?

// For security reasons, system("sync") is not a good idea.

sync();

sync();

sync();

FILE* f;

f = fopen("/proc/sys/vm/drop_caches", "w");

if (f == NULL) {

fprintf(stderr, "flush-cache: Couldn't open /proc/sys/vm/drop_caches\n");

exit(EXIT_FAILURE);

}

if (fprintf(f, "3\n") != 2) {

fprintf(stderr, "flush-cache: Couldn't write 3 to /proc/sys/vm/drop_caches\n");

exit(EXIT_FAILURE);

}

fclose(f);

printf("Done flushing.\n");

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值